In my webflux app I want to send some requests via WebClient. I want to handle all conditions (200, 401, 403 and ... response) and then response a json to client. for error status codes, I want to use @RestControllerAdvice, So I have to throw a custom exception and then in controller advice handle custom json. See the sample code:
WebClient.create().post
.uri("URI")
.retrieve()
.onStatus(HttpStatus::is4xxClientError, {
// create Mono<CustomException>
}
.bodyToMono(ResponseDto.class)
And now exception handler is as follow:
@ResponseBody
@ResponseStatus(...)
@ExceptionHandler(CustomException1.class)
public void customException1(CustomException1 exception) {
//do Something width response body
}
@ExceptionHandler(CustomException2.class)
public void customException2(CustomException2 exception) {
//do Something width response body
}
Webclient get a 401 response woth json body as follow:
{
"message": "Password is incorrect"
}
I can create Mono.error(new CustomException())
, But the problem is WebClient response body. If the message be "Password is incorrect", I want to send client:
Username or Password is incorrect
How do I do?
So I want do it with Exception Handler
. Webflux is not like spring boot, you solve rthings different using webflux. if you explain what it is you want to do more in detail when an exception is thrown. Do want to log? return a http status to the client? do a new request? – Toerktumlare