Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

599
Visualizações
Deserialize error response without object mapper in WebClient onErrorResume

I wonder if there is a better way to get error response in WebClient in a way that does not involve using additional ObjectMapper when calling onErrorResume?

In my WebClient:

import org.springframework.web.reactive.function.client.WebClientResponseException.BadRequest

// ...

.onErrorResume(BadRequest::class.java, badRequestToMyDto())
    private fun badRequestToMyDto(): (BadRequest) -> Mono<out MyDto> =
        { it: BadRequest ->
            val error = objectMapper.readValue<ErrorResponseDto>(it.responseBodyAsByteArray)
            // ...
}

There is a method .bodyToMono so I wonder if there is something similar that can be used in onErrorResume.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can use retrieve() in combination with onStatus to get access the client response when it is an "error". You can then invoke bodyToMono on it to unmarshall the response to a java object. Depending on the content type of the response body, spring will use the correct decoder. For examaple, if the body is json, it will use an ObjectMapper for unmarshalling.

WebClient.builder().build()
                .get()
                .uri("localhost:8080")
                .retrieve()
                .onStatus(HttpStatus::isError, clientResponse ->
                        clientResponse.bodyToMono(MyErrorResponse.class)
                                .flatMap(body -> {
                                    // logic
                                })
                )
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda