0

org.springframework.web.client.RestTemplate method exchance(..) throws RestClientException which is parent of org.springframework.web.client.HttpStatusCodeException, what are the possible scenario when restTeamplte.exchange(...) could throw this exception?

I checked the javadoc and tried to search it on internet but no luck.


try {
final ResponseEntity<String> response = applicationApiTemplate.exchange(uri,
                    HttpMethod.GET, entity, String.class);
} catch(HttpStatusCodeException e) {
// when possibly i will get this error?
}
vsk.rahul
  • 433
  • 1
  • 6
  • 11

1 Answers1

3

exhange(...) method of org.springframework.web.client.RestTemplate class was added in 3.1.0.RELEASE of spring-web library.

This method throws RestClientException which covers client (4_xx) and server (5_xx) side http code errors. But RestClientException doesn't offer getStatusCode(), getResponseAsString(), etc... methods.

  1. HttpsStatusCodeException is the child of RestClientException which is doing same thing but with additional methods like getStatusCode(), getResponseAsString(), etc.

  2. HttpClientErrorException child of HttpsStatusCodeException and only entertain client error (4_xx) not the server error.

  3. HttpServerErrorException child of HttpsStatusCodeException and only entertain server error (5_xx) not the client error