I use spring RestTemplateto use rest services (displayed in spring rest). I can use success scenarios. But for negative scenarios, the service returns error messages and error codes. I need to show these error messages on my web page.
For example, for an invalid request, service throws HttpStatus.BAD_REQUESTwith the correct messages. If I put a try-catch block, it jumps to the catch block, and I cannot get the object ResponseEntity.
try {
ResponseEntity<ResponseWrapper<MyEntity>> responseEntity = restTemplate.exchange(requestUrl, HttpMethod.POST, entity,
new ParameterizedTypeReference<ResponseWrapper<MyEntity>>() {
});
responseEntity.getStatusCode();
} catch (Exception e) {
e.printStackTrace();
}
How to receive ResponseWrapperfor an exception case?
I read about CustomRestTemplateand ResponseExtractorfrom here , but could not decide which one is best for my case.