dragon-zhang commented on code in PR #3776:
URL: https://github.com/apache/shenyu/pull/3776#discussion_r933930632
##########
shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/WebClientPlugin.java:
##########
@@ -49,27 +50,29 @@ public WebClientPlugin(final WebClient webClient) {
}
@Override
- protected Mono<ClientResponse> doRequest(final ServerWebExchange exchange,
final String httpMethod, final URI uri,
+ protected Mono<ResponseEntity<Void>> doRequest(final ServerWebExchange
exchange, final String httpMethod, final URI uri,
final HttpHeaders httpHeaders,
final Flux<DataBuffer> body) {
- // springWebflux5.3 mark #exchange() deprecated. because #echange
maybe make memory leak.
- // https://github.com/spring-projects/spring-framework/issues/25751
- // exchange is deprecated, so change to {@link
WebClient.RequestHeadersSpec#exchangeToMono(Function)}
- // exchangeToMono has two important bug:
- // 1.exchangeToMono can cause NPE when response body is null
- // 2.download file with exchangeToMono can't open
+ // Upgraded to Spring WebClient Retrieve utilizing RequestSpec &
ResponseSpec standard.
+ // OnStatus - is2xxSuccessful captures success responses
+ // OnStatus - isError captures all 4xx/5xx responses
+ // toBodilessEntity - captures 204 responses.
return webClient.method(HttpMethod.valueOf(httpMethod)).uri(uri)
.headers(headers -> headers.addAll(httpHeaders))
.body(BodyInserters.fromDataBuffers(body))
- .exchange()
- .doOnSuccess(res -> {
- if (res.statusCode().is2xxSuccessful()) {
-
exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE,
ResultEnum.SUCCESS.getName());
- } else {
-
exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE,
ResultEnum.ERROR.getName());
- }
+ .retrieve()
+ .onStatus(HttpStatus::is2xxSuccessful, res -> {
+
exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE,
ResultEnum.SUCCESS.getName());
exchange.getResponse().setStatusCode(res.statusCode());
exchange.getAttributes().put(Constants.CLIENT_RESPONSE_ATTR, res);
- });
+ return Mono.empty();
+ })
+ .onStatus(HttpStatus::isError, res -> {
Review Comment:
please use `status -> !status.is2xxSuccessful()` replace
`HttpStatus::isError`, otherwise, it is logically inconsistent.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]