Skip to content
Advertisement

Why handleResponse(URI url, HttpMethod method, ClientHttpResponse response) method of RestTemplate.class is getting called for a 200 Succeess response

I am calling an external API from my code using RestTemplate like below:

JavaScript

This API call is returning 200 Success but when I debug it, it still goes to handleResponse(URI url, HttpMethod method, ClientHttpResponse response) method of RestTemplate.class

And then it’s coming to my RestTemplateErrorHandler.java file

JavaScript

Can someone lease help me to understand if it’s the correct behaviour. I suspect that if the response is 200 Success it should not go to the RestTemlate.class and RestTemplateErrorHandler.class This behaviour is creating problem when API return 201 Created status, that time it goes to handleError() method and return the default case INTERNAL_SERVER_ERROR Can someone please help me here

Advertisement

Answer

The following code will call the error handler every time the response is not 200 OK, event if it is successful like 201 Created.

JavaScript

Try changing the implementation to the following:

JavaScript

This is better suited for your needs as it will consider all 2xx status as successful requests instead of only 200 OK.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement