I want to handle the Exception of my api by adding a WebExceptionHandler. I can change the status code, but I am stuck when i want to change the body of the response : ex adding
Given the answer, to serialize object I use this way :
Mono db = commonsException.getErrorsResponse().map(errorsResponse -> {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.writeValueAsBytes(errorsResponse);
} catch (JsonProcessingException e) {
return e.getMessage().getBytes();
}
}).map(s -> exchange.getResponse().bufferFactory().wrap(s));
exchange.getResponse().getHeaders().add("Content-Type", "application/json");
exchange.getResponse().setStatusCode(commonsException.getHttpStatus());
return exchange.getResponse().writeWith(db);