From ed4469f0812be728821e09886e3202983bd08bac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8D=95=E9=A3=8E=E7=9A=84=E9=80=8D=E9=81=A5=E4=BE=AF?= Date: Wed, 24 Jun 2020 15:22:58 +0800 Subject: [PATCH 1/3] webflux --- app/build.gradle | 2 +- .../springboot/common/exception/GlobalExceptionHandler.java | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 92785f6..e0b7d4b 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -64,7 +64,7 @@ springBoot { artifact = 'app' version = '1.2.3' group = 'hxy.cupb.springboot' - name = '简印用户服务端' + name = '捕风的逍遥侯gradle多module项目构建实例' } } } \ No newline at end of file diff --git a/common/src/main/java/hxy/cupb/springboot/common/exception/GlobalExceptionHandler.java b/common/src/main/java/hxy/cupb/springboot/common/exception/GlobalExceptionHandler.java index dbcee74..092f144 100755 --- a/common/src/main/java/hxy/cupb/springboot/common/exception/GlobalExceptionHandler.java +++ b/common/src/main/java/hxy/cupb/springboot/common/exception/GlobalExceptionHandler.java @@ -6,7 +6,6 @@ import org.slf4j.LoggerFactory; import org.springframework.dao.DuplicateKeyException; import org.springframework.http.HttpStatus; import org.springframework.web.bind.MethodArgumentNotValidException; -import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; -- Gitee From c8cdcab191e0094f09c4780070b5c5ed255db71c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8D=95=E9=A3=8E=E7=9A=84=E9=80=8D=E9=81=A5=E4=BE=AF?= Date: Wed, 24 Jun 2020 15:35:56 +0800 Subject: [PATCH 2/3] webflux -> web --- app/build.gradle | 4 +- app/src/test/resources/rest-api.http | 2 +- .../exception/GlobalExceptionHandler.java | 8 -- .../common/filter/CustomWebFilter.java | 36 --------- .../springboot/entity/vo/BaseResponseVO.java | 80 ++++++++++++++++++- 5 files changed, 82 insertions(+), 48 deletions(-) delete mode 100755 common/src/main/java/hxy/cupb/springboot/common/filter/CustomWebFilter.java diff --git a/app/build.gradle b/app/build.gradle index e0b7d4b..c694355 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,13 +16,13 @@ configurations { } dependencies { - implementation 'org.springframework.boot:spring-boot-starter-webflux' + implementation 'org.springframework.boot:spring-boot-starter-web' // implementation 'com.github.xiaoymin:knife4j-spring-boot-starter:2.0.1' compile project(':common') compile project(':entity') - testImplementation 'io.projectreactor:reactor-test' +// testImplementation 'io.projectreactor:reactor-test' } test { diff --git a/app/src/test/resources/rest-api.http b/app/src/test/resources/rest-api.http index e48e42d..117a624 100755 --- a/app/src/test/resources/rest-api.http +++ b/app/src/test/resources/rest-api.http @@ -1,4 +1,4 @@ -GET http://localhost:8080/user/list?PageSize=1 +GET http://localhost:8080/user/list?pageSize=10&pageNum=1 Accept: application/json ### diff --git a/common/src/main/java/hxy/cupb/springboot/common/exception/GlobalExceptionHandler.java b/common/src/main/java/hxy/cupb/springboot/common/exception/GlobalExceptionHandler.java index 092f144..42acea0 100755 --- a/common/src/main/java/hxy/cupb/springboot/common/exception/GlobalExceptionHandler.java +++ b/common/src/main/java/hxy/cupb/springboot/common/exception/GlobalExceptionHandler.java @@ -29,14 +29,6 @@ public class GlobalExceptionHandler { private static int PARAM_FAIL_CODE = 1002; private static int VALIDATION_CODE = 1003; - /** - * 处理自定义异常 - */ -// @ExceptionHandler(BizException.class) -// public BaseResponseVO handleRRException(BizException e) { -// logger.error(e.getMessage(), e); -// return new BaseResponseVO(e.getCode(), e.getMessage()); -// } /** * 方法参数校验 diff --git a/common/src/main/java/hxy/cupb/springboot/common/filter/CustomWebFilter.java b/common/src/main/java/hxy/cupb/springboot/common/filter/CustomWebFilter.java deleted file mode 100755 index d1e6c8f..0000000 --- a/common/src/main/java/hxy/cupb/springboot/common/filter/CustomWebFilter.java +++ /dev/null @@ -1,36 +0,0 @@ -package hxy.cupb.springboot.common.filter; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; -import org.springframework.web.method.HandlerMethod; -import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping; -import org.springframework.web.server.ServerWebExchange; -import org.springframework.web.server.WebFilter; -import org.springframework.web.server.WebFilterChain; -import reactor.core.publisher.Mono; - -import javax.validation.Valid; - -/** - * webflux已经没有了Interceptor的概念,但是可以通过WebFilter的方式实现. - */ -@Component -public class CustomWebFilter implements WebFilter { - - @Autowired - private RequestMappingHandlerMapping requestMappingHandlerMapping; - - @Override - public Mono filter(ServerWebExchange exchange, WebFilterChain chain) { - Object handlerMethod = requestMappingHandlerMapping.getHandler(exchange).toProcessor().peek(); - //注意跨域时的配置,跨域时浏览器会先发送一个option请求,这时候getHandler不会时真正的HandlerMethod - if(handlerMethod instanceof HandlerMethod){ - Valid valid = ((HandlerMethod) handlerMethod).getMethodAnnotation(Valid.class); - //do your logic - } - //preprocess() - Mono response = chain.filter(exchange); - //postprocess() - return response; - } -} \ No newline at end of file diff --git a/entity/src/main/java/hxy/cupb/springboot/entity/vo/BaseResponseVO.java b/entity/src/main/java/hxy/cupb/springboot/entity/vo/BaseResponseVO.java index 74caae7..edcf658 100755 --- a/entity/src/main/java/hxy/cupb/springboot/entity/vo/BaseResponseVO.java +++ b/entity/src/main/java/hxy/cupb/springboot/entity/vo/BaseResponseVO.java @@ -2,6 +2,8 @@ package hxy.cupb.springboot.entity.vo; import lombok.Data; +import java.io.Serializable; + /** * @author eric * @program springboot-gradle-basic @@ -9,7 +11,12 @@ import lombok.Data; * @date 2020/2/17 */ @Data -public class BaseResponseVO { +public class BaseResponseVO implements Serializable { + private static final long serialVersionUID = 6610930573200798624L; + /** + * 时间戳 + */ + private long timestamp = System.currentTimeMillis(); private String msg; private Integer code; @@ -55,4 +62,75 @@ public class BaseResponseVO { public static BaseResponseVO success(String msg, T data) { return new BaseResponseVO(200, msg, data); } + + + +// public static BaseResponseVO success(T data) { +// return new BaseResponseVO(StatusCodeEnum.SUCCESS.getCode(), "success", data); +// } +// +// public static BaseResponseVO success(String message, T data) { +// return new BaseResponseVO(SUCCESS.getCode(), message, data); +// } +// +// public static BaseResponseVO error() { +// return new BaseResponseVO(FAIL.getCode(), FAIL.getDescription(), null); +// } +// +// public static BaseResponseVO error(String message) { +// return new BaseResponseVO(FAIL.getCode(), message, null); +// } +// +// public static BaseResponseVO error(T data) { +// return new BaseResponseVO(FAIL.getCode(), FAIL.getDescription(), data); +// } +// +// public static BaseResponseVO error(String message, T data) { +// return new BaseResponseVO(FAIL.getCode(), message, data); +// } +// +// public static BaseResponseVO notFound(String message, T data) { +// return new BaseResponseVO(DATA_NOT_FOUND.getCode(), message, data); +// } +// +// public static BaseResponseVO notFound(T data) { +// return new BaseResponseVO(DATA_NOT_FOUND.getCode(), DATA_NOT_FOUND.getDescription(), data); +// } +// +// public static BaseResponseVO deleteWarn(T data) { +// return new BaseResponseVO(DELETE_WARN.getCode(), DELETE_WARN.getDescription(), data); +// } +// +// /** +// * 请求错误 +// * +// * @param data +// * @param +// * @return +// */ +// public static BaseResponseVO badRequest(T data) { +// return new BaseResponseVO(PARAM_BASE_CHECK_FAIL.getCode(), PARAM_BASE_CHECK_FAIL.getDescription(), data); +// } +// +// +// public static BaseResponseVO badrequest() { +// return new BaseResponseVO(PARAM_BASE_CHECK_FAIL.getCode(), "no identifier arguments", null); +// } +// +// public static BaseResponseVO badrequest(String message) { +// return new BaseResponseVO(PARAM_BASE_CHECK_FAIL.getCode(), message, null); +// } +// +// public static BaseResponseVO badrequest(T data) { +// return new BaseResponseVO(PARAM_BASE_CHECK_FAIL.getCode(), "no identifier arguments", data); +// } +// +// public static BaseResponseVO badrequest(String message, T data) { +// return new BaseResponseVO(PARAM_BASE_CHECK_FAIL.getCode(), message, data); +// } +// +// public static BaseResponseVO noLogin(String message) { +// return new BaseResponseVO(UNAUTHORIZED.getCode(), message, null); +// } + } -- Gitee From f0b75570e7b51c77a82ef8f7d5113992bc2d3632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8D=95=E9=A3=8E=E7=9A=84=E9=80=8D=E9=81=A5=E4=BE=AF?= Date: Thu, 25 Jun 2020 20:39:20 +0800 Subject: [PATCH 3/3] update --- app/build.gradle | 4 +-- .../common/filter/CustomWebFilter.java | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 common/src/main/java/hxy/cupb/springboot/common/filter/CustomWebFilter.java diff --git a/app/build.gradle b/app/build.gradle index c694355..e0b7d4b 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,13 +16,13 @@ configurations { } dependencies { - implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-webflux' // implementation 'com.github.xiaoymin:knife4j-spring-boot-starter:2.0.1' compile project(':common') compile project(':entity') -// testImplementation 'io.projectreactor:reactor-test' + testImplementation 'io.projectreactor:reactor-test' } test { diff --git a/common/src/main/java/hxy/cupb/springboot/common/filter/CustomWebFilter.java b/common/src/main/java/hxy/cupb/springboot/common/filter/CustomWebFilter.java new file mode 100644 index 0000000..d1e6c8f --- /dev/null +++ b/common/src/main/java/hxy/cupb/springboot/common/filter/CustomWebFilter.java @@ -0,0 +1,36 @@ +package hxy.cupb.springboot.common.filter; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.method.HandlerMethod; +import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping; +import org.springframework.web.server.ServerWebExchange; +import org.springframework.web.server.WebFilter; +import org.springframework.web.server.WebFilterChain; +import reactor.core.publisher.Mono; + +import javax.validation.Valid; + +/** + * webflux已经没有了Interceptor的概念,但是可以通过WebFilter的方式实现. + */ +@Component +public class CustomWebFilter implements WebFilter { + + @Autowired + private RequestMappingHandlerMapping requestMappingHandlerMapping; + + @Override + public Mono filter(ServerWebExchange exchange, WebFilterChain chain) { + Object handlerMethod = requestMappingHandlerMapping.getHandler(exchange).toProcessor().peek(); + //注意跨域时的配置,跨域时浏览器会先发送一个option请求,这时候getHandler不会时真正的HandlerMethod + if(handlerMethod instanceof HandlerMethod){ + Valid valid = ((HandlerMethod) handlerMethod).getMethodAnnotation(Valid.class); + //do your logic + } + //preprocess() + Mono response = chain.filter(exchange); + //postprocess() + return response; + } +} \ No newline at end of file -- Gitee