gpt4 book ai didi

java - Spring Reactive Web - 异常总是包含在 500 中

转载 作者:行者123 更新时间:2023-12-04 08:58:59 25 4
gpt4 key购买 nike

对于一个非常简单的项目,错误处理不清楚。

public class WebfluxApplication {
public static void main(String[] args) {
SpringApplication.run(WebfluxApplication.class, args);
}
}
  • public class EndpointRouter
    {

    @Bean
    public WebClient webClient() {
    return WebClient.builder().build();
    }

    @Bean
    public RouterFunction<ServerResponse> goodRoute(Handler handler, ConfigProperties configProperties) {
    log.info(configProperties.toString());
    return
    RouterFunctions.route(RequestPredicates.GET("/api/v1/integration/ok"),
    handler::goodEndpoint)
    .and(
    RouterFunctions.route(RequestPredicates.GET("/api/v1/integration/notfound") {
    handler::badEndpoint));
    }
  • public Mono<ServerResponse> goodEndpoint(ServerRequest r) {
    return ServerResponse.ok().build();
    }

    public Mono<ServerResponse> badEndpoint(ServerRequest r) {
    var result = service.badEndpoint();
    return ServerResponse
    .ok()
    .body(result, String.class);
    }
  • public class Service
    {
    private final WebClient webClient;
    private final ConfigProperties configProperties;

    public Service(WebClient webClient, ConfigProperties configurationProperties) {
    this.webClient = webClient;
    this.configProperties = configurationProperties;
    }

    public Mono<String> badEndpoint() {
    return webClient
    .get()
    .uri(configProperties.getNotfound())
    .retrieve()
    .onStatus(HttpStatus::is4xxClientError, clientResponse -> {
    if(clientResponse.statusCode().equals(HttpStatus.NOT_FOUND)){
    return Mono.error(new HttpClientErrorException(HttpStatus.NOT_FOUND,
    "Entity not found."));
    } else {
    return Mono.error(new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR));
    }
    })
    .bodyToMono(String.class);
    }
    通过阅读文档,我不需要为整个项目设置全局错误处理程序,我应该能够处理 404,并将 404 返回给原始调用者。
    这是输出
       2020-08-29 16:52:46.301 ERROR 25020 --- [ctor-http-nio-4] a.w.r.e.AbstractErrorWebExceptionHandler : [b339763e-1]  500 Server Error for HTTP GET "/api/v1/integration/notfound"

    org.springframework.web.client.HttpClientErrorException: 404 Entity not found.
    at com.stevenpg.restperformance.webflux.Service.lambda$badEndpoint$0(Service.java:30) ~[main/:na]
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
    Error has been observed at the following site(s):
    |_ checkpoint ⇢ 404 from GET https://httpbin.org/status/404 [DefaultWebClient]
    |_ checkpoint ⇢ Handler com.stevenpg.restperformance.webflux.EndpointRouter$$Lambda$445/0x00000008003ae040@7799b58 [DispatcherHandler]
    |_ checkpoint ⇢ HTTP GET "/api/v1/integration/notfound" [ExceptionHandlingWebHandler]
    我也试过在我的 Mono 上从服务中使用 onErrorResume,但它永远无法正常工作,并且需要返回 Mono 而不是 Mono。
    文档和 Stack Overflow 没有很多/任何在 RouterFunction 内进行 WebClient 调用和处理不同类型响应的示例。

    最佳答案

    只需添加 onErrorResume 即可解决您的问题。否则在 AbstractErrorWebExceptionHandler 中处理错误。

     return webClient
    .get()
    .uri(configProperties.getNotfound())
    .retrieve()
    .onStatus(HttpStatus::is4xxClientError, clientResponse -> {
    if(clientResponse.statusCode().equals(HttpStatus.NOT_FOUND)){
    return Mono.error(new HttpClientErrorException(HttpStatus.NOT_FOUND,
    "Entity not found."));
    } else {
    return Mono.error(new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR));
    }
    })
    .bodyToMono(String.class)
    .onErrorResume( e -> Mono.just(e.getMessage()) );

    关于java - Spring Reactive Web - 异常总是包含在 500 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63651723/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com