Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

600
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda