Spring Handling Client Client Exceptions

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) {
        //TODO How to get response here, so that i can get error messages?
        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.

+4
2

. HttpClientErrorException .

e.getResponseBodyAsString(), ResponseBody.

+8

:

HttpClientErrorException (HTTP-s >= 400) HttpServerErrorException (HTTP-s >= 500) RestClientException.

, ErrorHandler,

+3

All Articles