Commit 48f40cbdd82e67e8170f21d06d18bf22c6389b23
1 parent
e347e5b2
代码格式化
Showing
17 changed files
with
20 additions
and
80 deletions
src/main/java/com/diligrp/xtrade/gateway/application/TestAggregationApplication.java
... | ... | @@ -9,7 +9,7 @@ import org.springframework.http.server.reactive.ServerHttpRequest; |
9 | 9 | import org.springframework.stereotype.Component; |
10 | 10 | import org.springframework.validation.annotation.Validated; |
11 | 11 | |
12 | -import javax.validation.groups.Default; | |
12 | +import javax.servlet.http.HttpServletRequest; | |
13 | 13 | |
14 | 14 | /** |
15 | 15 | * @Auther: miaoguoxin |
... | ... | @@ -23,9 +23,11 @@ public class TestAggregationApplication { |
23 | 23 | |
24 | 24 | @Autowired |
25 | 25 | private ServerHttpRequest serverHttpRequest; |
26 | + @Autowired | |
27 | + private HttpServletRequest httpServletRequest; | |
26 | 28 | |
27 | 29 | @DispatchMapping("/test") |
28 | - public String test(DispatchContext<TestRequestDto> dispatchContext){ | |
30 | + public String test(DispatchContext<TestRequestDto> dispatchContext) { | |
29 | 31 | return "ffff"; |
30 | 32 | } |
31 | 33 | ... | ... |
src/main/java/com/diligrp/xtrade/gateway/common/constant/GatewayConst.java
src/main/java/com/diligrp/xtrade/gateway/common/utils/ResponseUtils.java
... | ... | @@ -18,15 +18,12 @@ public class ResponseUtils { |
18 | 18 | |
19 | 19 | |
20 | 20 | public static Mono<Void> writeForbidden(ServerHttpResponse response){ |
21 | - Message message = Message | |
22 | - .builder() | |
23 | - .code(HttpStatus.FORBIDDEN.value()) | |
24 | - .message(HttpStatus.FORBIDDEN.toString()) | |
25 | - .build(); | |
21 | + Message<Object> message = Message.failure( | |
22 | + HttpStatus.FORBIDDEN.value(),HttpStatus.FORBIDDEN.toString()); | |
26 | 23 | return writeResponse(response,message); |
27 | 24 | } |
28 | 25 | |
29 | - public static Mono<Void> writeResponse(ServerHttpResponse response,Message message){ | |
26 | + public static Mono<Void> writeResponse(ServerHttpResponse response,Message<Object> message){ | |
30 | 27 | String respJson = JsonUtils.toJsonString(message); |
31 | 28 | response.getHeaders().setContentLength( |
32 | 29 | respJson.getBytes(StandardCharsets.UTF_8).length); | ... | ... |
src/main/java/com/diligrp/xtrade/gateway/common/utils/ValidateUtils.java
... | ... | @@ -5,7 +5,6 @@ import org.springframework.beans.factory.annotation.Autowired; |
5 | 5 | import org.springframework.stereotype.Component; |
6 | 6 | |
7 | 7 | import javax.validation.ConstraintViolation; |
8 | -import javax.validation.Validation; | |
9 | 8 | import javax.validation.Validator; |
10 | 9 | import javax.validation.groups.Default; |
11 | 10 | import java.util.Arrays; | ... | ... |
src/main/java/com/diligrp/xtrade/gateway/config/property/AuthPathProperties.java
... | ... | @@ -4,13 +4,9 @@ import com.diligrp.xtrade.gateway.common.utils.PathUtils; |
4 | 4 | import lombok.Data; |
5 | 5 | import org.springframework.boot.context.properties.ConfigurationProperties; |
6 | 6 | import org.springframework.stereotype.Component; |
7 | -import org.springframework.util.CollectionUtils; | |
8 | 7 | import org.springframework.web.util.pattern.PathPattern; |
9 | -import org.springframework.web.util.pattern.PathPatternParser; | |
10 | 8 | |
11 | -import java.util.ArrayList; | |
12 | 9 | import java.util.List; |
13 | -import java.util.concurrent.CopyOnWriteArrayList; | |
14 | 10 | |
15 | 11 | /** |
16 | 12 | * @Auther: miaoguoxin | ... | ... |
src/main/java/com/diligrp/xtrade/gateway/controller/TestController.java
... | ... | @@ -2,29 +2,19 @@ package com.diligrp.xtrade.gateway.controller; |
2 | 2 | |
3 | 3 | import com.diligrp.xtrade.gateway.domain.TestRequestDto; |
4 | 4 | import com.diligrp.xtrade.shared.domain.Message; |
5 | -import com.diligrp.xtrade.shared.util.DateUtils; | |
6 | -import com.google.common.collect.Lists; | |
7 | 5 | import org.springframework.beans.factory.annotation.Autowired; |
8 | 6 | import org.springframework.beans.factory.annotation.Value; |
9 | 7 | import org.springframework.cloud.context.config.annotation.RefreshScope; |
10 | 8 | import org.springframework.cloud.gateway.config.GatewayProperties; |
11 | 9 | import org.springframework.cloud.gateway.route.RouteDefinition; |
12 | -import org.springframework.http.server.PathContainer; | |
13 | 10 | import org.springframework.validation.annotation.Validated; |
14 | 11 | import org.springframework.web.bind.annotation.GetMapping; |
15 | 12 | import org.springframework.web.bind.annotation.PathVariable; |
16 | 13 | import org.springframework.web.bind.annotation.RequestMapping; |
17 | 14 | import org.springframework.web.bind.annotation.RestController; |
18 | -import org.springframework.web.util.pattern.PathPattern; | |
19 | -import org.springframework.web.util.pattern.PathPatternParser; | |
20 | 15 | |
21 | 16 | import javax.validation.constraints.Min; |
22 | -import javax.validation.constraints.NotBlank; | |
23 | -import javax.validation.groups.Default; | |
24 | -import java.time.LocalDateTime; | |
25 | -import java.util.ArrayList; | |
26 | 17 | import java.util.List; |
27 | -import java.util.Optional; | |
28 | 18 | |
29 | 19 | /** |
30 | 20 | * @Auther: miaoguoxin |
... | ... | @@ -41,12 +31,6 @@ public class TestController { |
41 | 31 | @Value("${ribbon.ServerListRefreshInterval}") |
42 | 32 | private String interval; |
43 | 33 | |
44 | - @GetMapping("/gateway/{id}") | |
45 | - public Message test(@Min(value = 5,message = "id不能小于5") @PathVariable Integer id) { | |
46 | - List<RouteDefinition> routes = gatewayProperties.getRoutes(); | |
47 | - return Message.builder().success(); | |
48 | - } | |
49 | - | |
50 | 34 | @GetMapping("/test_dynamic") |
51 | 35 | public Message<String> test1( TestRequestDto testRequestDto) { |
52 | 36 | throw new RuntimeException("fffff"); | ... | ... |
src/main/java/com/diligrp/xtrade/gateway/domain/TestRequestDto.java
src/main/java/com/diligrp/xtrade/gateway/exception/GlobalGatewayErrorHandler.java
... | ... | @@ -111,10 +111,7 @@ public class GlobalGatewayErrorHandler implements ErrorWebExceptionHandler { |
111 | 111 | } |
112 | 112 | |
113 | 113 | private Map<String, Object> getHttpResult(HttpStatus httpStatus) { |
114 | - Message apiResult= Message.builder() | |
115 | - .code(httpStatus.value()) | |
116 | - .message(httpStatus.toString()) | |
117 | - .build(); | |
114 | + Message<String> apiResult= Message.failure(httpStatus.value(),httpStatus.toString()); | |
118 | 115 | Map<String,Object> result=new HashMap<>(); |
119 | 116 | result.put("httpStatus",httpStatus); |
120 | 117 | result.put("body", JSON.toJSONString(apiResult)); |
... | ... | @@ -128,10 +125,7 @@ public class GlobalGatewayErrorHandler implements ErrorWebExceptionHandler { |
128 | 125 | Map<String, Object> errorAttrs = errorAttributes.getErrorAttributes(request, true); |
129 | 126 | String msg = String.format("%s %s", |
130 | 127 | errorAttrs.get("path"), errorAttrs.get("error")); |
131 | - Message message = Message.builder() | |
132 | - .code((int)errorAttrs.get("status")) | |
133 | - .message(msg) | |
134 | - .build(); | |
128 | + Message<Object> message = Message.failure((int)errorAttrs.get("status"),msg); | |
135 | 129 | if (message.getCode().equals(500)){ |
136 | 130 | log.error("[全局异常处理]异常请求路径:{},记录异常信息:{}", errorAttrs.get("path"),errorAttributes.getError(request)); |
137 | 131 | } | ... | ... |
src/main/java/com/diligrp/xtrade/gateway/filters/factory/DispatchGatewayFilterFactory.java
... | ... | @@ -5,16 +5,12 @@ import com.diligrp.xtrade.gateway.common.utils.ResponseUtils; |
5 | 5 | import com.diligrp.xtrade.gateway.support.dispatch.RequestDispatcher; |
6 | 6 | import com.diligrp.xtrade.shared.domain.Message; |
7 | 7 | import lombok.Data; |
8 | -import org.reactivestreams.Publisher; | |
9 | 8 | import org.springframework.beans.factory.annotation.Autowired; |
10 | 9 | import org.springframework.cloud.gateway.filter.GatewayFilter; |
11 | 10 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; |
12 | 11 | import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; |
13 | 12 | import org.springframework.core.Ordered; |
14 | -import org.springframework.core.io.buffer.DataBuffer; | |
15 | -import org.springframework.http.HttpHeaders; | |
16 | 13 | import org.springframework.http.MediaType; |
17 | -import org.springframework.http.server.reactive.ServerHttpResponseDecorator; | |
18 | 14 | import org.springframework.stereotype.Component; |
19 | 15 | import org.springframework.web.server.ServerWebExchange; |
20 | 16 | import reactor.core.publisher.Mono; | ... | ... |
src/main/java/com/diligrp/xtrade/gateway/filters/global/ResponseReadBodyGlobalFilter.java
1 | -package com.diligrp.xtrade.gateway.filters.global; | |
1 | +package com.diligrp.xtrade.gateway.filters.global ; | |
2 | 2 | |
3 | 3 | import lombok.extern.slf4j.Slf4j; |
4 | 4 | import org.reactivestreams.Publisher; |
... | ... | @@ -29,7 +29,7 @@ import java.util.function.BiFunction; |
29 | 29 | |
30 | 30 | import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR; |
31 | 31 | |
32 | -/** | |
32 | +/* | |
33 | 33 | * @auther: miaoguoxin |
34 | 34 | * @date: 2018/12/26 21:26 |
35 | 35 | * @description: 读取响应体过滤器 |
... | ... | @@ -37,7 +37,6 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.O |
37 | 37 | @Component |
38 | 38 | @Slf4j |
39 | 39 | public class ResponseReadBodyGlobalFilter implements GlobalFilter, Ordered { |
40 | - | |
41 | 40 | private final List<HttpMessageReader<?>> messageReaders; |
42 | 41 | |
43 | 42 | public ResponseReadBodyGlobalFilter() { |
... | ... | @@ -62,10 +61,6 @@ public class ResponseReadBodyGlobalFilter implements GlobalFilter, Ordered { |
62 | 61 | |
63 | 62 | /** |
64 | 63 | * 响应body处理,添加响应的打印 |
65 | - * | |
66 | - * @param exchange | |
67 | - * @param startTime | |
68 | - * @return | |
69 | 64 | */ |
70 | 65 | private BodyHandlerServerHttpResponseDecorator.BodyHandlerFunction initBodyHandler(ServerWebExchange exchange, long startTime) { |
71 | 66 | return (resp, body) -> { |
... | ... | @@ -77,7 +72,7 @@ public class ResponseReadBodyGlobalFilter implements GlobalFilter, Ordered { |
77 | 72 | HttpHeaders httpHeaders = new HttpHeaders(); |
78 | 73 | httpHeaders.setContentType(originalResponseContentType); |
79 | 74 | |
80 | - ClientResponse clientResponse = this.prepareClientResponse(exchange.getResponse().getStatusCode(),body, httpHeaders); | |
75 | + ClientResponse clientResponse = this.prepareClientResponse(exchange.getResponse().getStatusCode(), body, httpHeaders); | |
81 | 76 | Mono<String> bodyMono = clientResponse.bodyToMono(String.class); |
82 | 77 | return bodyMono.flatMap(respBody -> { |
83 | 78 | //打印返回响应日志 |
... | ... | @@ -96,8 +91,6 @@ public class ResponseReadBodyGlobalFilter implements GlobalFilter, Ordered { |
96 | 91 | }; |
97 | 92 | } |
98 | 93 | |
99 | - | |
100 | - | |
101 | 94 | private ClientResponse prepareClientResponse(HttpStatus httpStatus, Publisher<? extends DataBuffer> body, |
102 | 95 | HttpHeaders httpHeaders) { |
103 | 96 | return ClientResponse.create(httpStatus, messageReaders) |
... | ... | @@ -106,7 +99,7 @@ public class ResponseReadBodyGlobalFilter implements GlobalFilter, Ordered { |
106 | 99 | } |
107 | 100 | |
108 | 101 | |
109 | - protected static class BodyHandlerServerHttpResponseDecorator extends ServerHttpResponseDecorator{ | |
102 | + protected static class BodyHandlerServerHttpResponseDecorator extends ServerHttpResponseDecorator { | |
110 | 103 | |
111 | 104 | interface BodyHandlerFunction extends BiFunction<ServerHttpResponse, Publisher<? extends DataBuffer>, Mono<Void>> { |
112 | 105 | |
... | ... | @@ -116,11 +109,9 @@ public class ResponseReadBodyGlobalFilter implements GlobalFilter, Ordered { |
116 | 109 | * body 处理拦截器 |
117 | 110 | */ |
118 | 111 | private BodyHandlerServerHttpResponseDecorator.BodyHandlerFunction bodyHandler = initDefaultBodyHandler(); |
112 | + | |
119 | 113 | /** |
120 | 114 | * 构造函数 |
121 | - * | |
122 | - * @param bodyHandler | |
123 | - * @param delegate | |
124 | 115 | */ |
125 | 116 | public BodyHandlerServerHttpResponseDecorator(BodyHandlerServerHttpResponseDecorator.BodyHandlerFunction bodyHandler, ServerHttpResponse delegate) { |
126 | 117 | super(delegate); |
... | ... | @@ -145,7 +136,6 @@ public class ResponseReadBodyGlobalFilter implements GlobalFilter, Ordered { |
145 | 136 | /** |
146 | 137 | * 默认body拦截处理器 |
147 | 138 | * |
148 | - * @return | |
149 | 139 | */ |
150 | 140 | private BodyHandlerServerHttpResponseDecorator.BodyHandlerFunction initDefaultBodyHandler() { |
151 | 141 | return ReactiveHttpOutputMessage::writeWith; | ... | ... |
src/main/java/com/diligrp/xtrade/gateway/filters/web/ReactiveContextWebFilter.java
... | ... | @@ -9,9 +9,6 @@ import org.springframework.web.server.ServerWebExchange; |
9 | 9 | import org.springframework.web.server.WebFilter; |
10 | 10 | import org.springframework.web.server.WebFilterChain; |
11 | 11 | import reactor.core.publisher.Mono; |
12 | -import reactor.core.publisher.SignalType; | |
13 | - | |
14 | -import java.util.function.Consumer; | |
15 | 12 | |
16 | 13 | /** |
17 | 14 | * @Auther: miaoguoxin | ... | ... |
src/main/java/com/diligrp/xtrade/gateway/predicates/ReadBodyRoutePredicateFactory.java
... | ... | @@ -9,17 +9,13 @@ import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; |
9 | 9 | import org.springframework.http.HttpHeaders; |
10 | 10 | import org.springframework.http.codec.HttpMessageReader; |
11 | 11 | import org.springframework.http.server.reactive.ServerHttpRequest; |
12 | -import org.springframework.stereotype.Component; | |
13 | 12 | import org.springframework.web.reactive.function.server.HandlerStrategies; |
14 | 13 | import org.springframework.web.reactive.function.server.ServerRequest; |
15 | 14 | import org.springframework.web.server.ServerWebExchange; |
16 | -import reactor.core.Disposable; | |
17 | 15 | import reactor.core.publisher.Mono; |
18 | 16 | |
19 | -import java.time.Duration; | |
20 | 17 | import java.util.List; |
21 | 18 | import java.util.function.Consumer; |
22 | -import java.util.function.Function; | |
23 | 19 | import java.util.function.Predicate; |
24 | 20 | |
25 | 21 | import static com.diligrp.xtrade.gateway.common.constant.GatewayConst.CACHE_REQUEST_BODY_OBJECT_KEY; |
... | ... | @@ -74,12 +70,7 @@ public class ReadBodyRoutePredicateFactory extends AbstractRoutePredicateFactory |
74 | 70 | .create(exchange.mutate().request(serverHttpRequest) |
75 | 71 | .build(), messageReaders) |
76 | 72 | .bodyToMono(String.class) |
77 | - .doOnNext(new Consumer<String>() { | |
78 | - @Override | |
79 | - public void accept(String objectValue) { | |
80 | - exchange.getAttributes().put(CACHE_REQUEST_BODY_OBJECT_KEY, objectValue); | |
81 | - } | |
82 | - }) | |
73 | + .doOnNext(objectValue -> exchange.getAttributes().put(CACHE_REQUEST_BODY_OBJECT_KEY, objectValue)) | |
83 | 74 | .map(objectValue -> Boolean.TRUE)); |
84 | 75 | }; |
85 | 76 | } | ... | ... |
src/main/java/com/diligrp/xtrade/gateway/repository/GatewayRepository.java
... | ... | @@ -17,7 +17,6 @@ import org.springframework.cloud.gateway.route.RouteDefinition; |
17 | 17 | import org.springframework.stereotype.Repository; |
18 | 18 | import org.springframework.util.CollectionUtils; |
19 | 19 | import org.springframework.web.util.UriComponentsBuilder; |
20 | -import reactor.core.publisher.Mono; | |
21 | 20 | |
22 | 21 | import java.lang.reflect.InvocationTargetException; |
23 | 22 | import java.net.URI; | ... | ... |
src/main/java/com/diligrp/xtrade/gateway/route/impl/CloudRouteResourceLoader.java
... | ... | @@ -39,7 +39,6 @@ public class CloudRouteResourceLoader implements DynamicRouteLoaderIntf { |
39 | 39 | private final static String GROUP = "DEFAULT_GROUP"; |
40 | 40 | private final static String ROUTE_CONFIG_NAME = "route.properties"; |
41 | 41 | |
42 | - | |
43 | 42 | @Value("${spring.application.name}") |
44 | 43 | private String applicationName; |
45 | 44 | @Value("${spring.profiles.active}") | ... | ... |
src/main/java/com/diligrp/xtrade/gateway/support/dispatch/DispatchContext.java
... | ... | @@ -10,10 +10,10 @@ import org.springframework.http.server.reactive.ServerHttpResponse; |
10 | 10 | */ |
11 | 11 | @Getter |
12 | 12 | public class DispatchContext<T> { |
13 | - private ServerHttpRequest request; | |
14 | - private ServerHttpResponse response; | |
13 | + private final ServerHttpRequest request; | |
14 | + private final ServerHttpResponse response; | |
15 | 15 | /**请求参数*/ |
16 | - private T param; | |
16 | + private final T param; | |
17 | 17 | |
18 | 18 | public DispatchContext(ServerHttpRequest request, ServerHttpResponse response, T param) { |
19 | 19 | this.request = request; | ... | ... |
src/main/java/com/diligrp/xtrade/gateway/support/dispatch/MappingMetaInfo.java
... | ... | @@ -12,9 +12,9 @@ import java.lang.reflect.Method; |
12 | 12 | @Getter |
13 | 13 | public class MappingMetaInfo { |
14 | 14 | /**方法所属的bean class*/ |
15 | - private Class<?> targetClass; | |
15 | + private final Class<?> targetClass; | |
16 | 16 | /** 需要执行的方法 */ |
17 | - private Method targetMethod; | |
17 | + private final Method targetMethod; | |
18 | 18 | /***/ |
19 | 19 | private Validated validated; |
20 | 20 | /**泛化参数类型 {@link DispatchContext#getParam()}*/ | ... | ... |