简体   繁体   English

Kotlin Spring Reactive Webflux - 处理 WebClient 错误

[英]Kotlin Spring Reactive Webflux - Handle WebClient Error

I am having troubles trying to handle different errors from calling Spring webflux's web client.我在尝试处理与调用 Spring webflux 的 Web 客户端不同的错误时遇到了麻烦。

Below is my current code.下面是我目前的代码。

    return request
            .bodyToMono(InputMessage::class.java)
            .flatMap { inputMessage ->
                client
                        .get()
                        .uri { builder ->
                            builder.path("/message")
                                    .queryParam("message", inputMessage.message)
                                    .build()
                        }
                        .retrieve()
                        .onStatus({t: HttpStatus -> t.is5xxServerError}, {c: ClientResponse -> Mono.error(Throwable("Internal Server Error - try again later"))})
                        .bodyToMono(ListOfAddresses::class.java)
            }
            .flatMap { s -> ServerResponse.ok().syncBody(s) }

If it errors, it is still returning the full error message from the client's call.如果出错,它仍会从客户端调用返回完整的错误消息。

I tried something else, like this我尝试了别的东西,像这样

    return request
            .bodyToMono(InputMessage::class.java)
            .flatMap { inputMessage ->
                client
                        .get()
                        .uri { builder ->
                            builder.path("/message")
                                    .queryParam("message", inputMessage.message)
                                    .build()
                        }
                        .retrieve()
                        .onStatus({t: HttpStatus -> t.is5xxServerError}, {c: ClientResponse -> Mono.error(Throwable("Internal Server Error - try again later"))})
                        .bodyToMono(ListOfAddresses::class.java)
            }
            .flatMap { s -> ServerResponse.ok().syncBody(s) }
            .onErrorResume { e -> Mono.just("Error " + e.message)
                    .flatMap { s -> ServerResponse.ok().syncBody(s) } }

It actually works but then I want to handle different Http status codes error (different messages for each Http status code).它实际上有效,但后来我想处理不同的 Http 状态代码错误(每个 Http 状态代码的不同消息)。

How can I modify my code so it will return the custom message I build?如何修改我的代码以便它返回我构建的自定义消息?

As per WebFlux documentation, you can user the exchangeToMono() or awaitExchange { } in order to have an error handling.根据WebFlux文档,您可以使用exchangeToMono()awaitExchange { }以进行错误处理。

Mono<Object> entityMono = client.get()
    .uri("/persons/1")
    .accept(MediaType.APPLICATION_JSON)
    .exchangeToMono(response -> {
        if (response.statusCode().equals(HttpStatus.OK)) {
            return response.bodyToMono(Person.class);
        }
        else if (response.statusCode().is4xxClientError()) {
            // Suppress error status code
            return response.bodyToMono(ErrorContainer.class);
        }
        else {
            // Turn to error
            return response.createException().flatMap(Mono::error);
        }
    });

Code copied from WebFlux link above.从上面的 WebFlux 链接复制的代码。

Take a look at 2.3.看看2.3。 Exchange 交换

val entity = client.get()
  .uri("/persons/1")
  .accept(MediaType.APPLICATION_JSON)
  .awaitExchange {
        if (response.statusCode() == HttpStatus.OK) {
             return response.awaitBody<Person>()
        }
        else if (response.statusCode().is4xxClientError) {
             return response.awaitBody<ErrorContainer>()
        }
        else {
             throw response.createExceptionAndAwait()
        }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM