In my controller I call a service layer to return the result of a webflux webclient which in turn sends requests to another microservice.
The microservice can either return the matching Object or potentially throw an exception which comes back in application/json+problem header.
My controller is simply the following:
public ResponseEntity<MyDTO> create(@Valid @RequestBody final MyDTO myDTO) throws URISyntaxException {
log.debug("REST request to create : {}", myDTO);
Object response = myService.create();
MyDTOresult = new ObjectMapper().convertValue(response , MyDTO.class);
return ResponseEntity.ok().body(result);
}
And my webclient service layer:
webClient
.post()
.uri(MY_URL)
.accept(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(myDTO))
.retrieve()
.onStatus(HttpStatus::isError, response -> response.bodyToMono(String.class)
.flatMap(error -> Mono.error(new RuntimeException(error))))
.bodyToMono(Object.class)
.block();
The issue I'm facing is that if the exception handling is not being triggered, not sure what I'm missing here as essentially I want a generic exception parser to catch the unsuccessful responses and stop the process there and then, but at the moment I think this is attempting to map it to Object.class, (Content type 'application/problem+json' not supported for bodyType=java.lang.Object).
Thanks
question from:
https://stackoverflow.com/questions/65883559/how-to-handle-return-type-and-exception-on-spring-webflux-webclient 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…