Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When HttpStatusCodeException exception raised?

when i use below code , what is the case to get HttpStatusCodeException exception .

ResponseEntity<Object> response = 
  restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, entity, Object.class);

Please can anyone help out ????????

like image 227
Nag-Raj Avatar asked Jan 24 '26 19:01

Nag-Raj


2 Answers

According to documentation there are two types of HttpStatusCodeException HttpClientErrorException and HttpServerErrorException.

  • The former is thrown when an HTTP 4xx is received.
  • The latter is thrown when an HTTP 5xx is received.

so you can just use ResponseEntity.BodyBuilder.status(505) for example to raise an HttpServerErrorException in your

like image 178
gtosto Avatar answered Jan 26 '26 10:01

gtosto


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.

like image 37
vsk.rahul Avatar answered Jan 26 '26 08:01

vsk.rahul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!