When HttpStatusCodeException exception raised?

We Are Going To Discuss About When HttpStatusCodeException exception raised?. So lets Start this Java Article.

When HttpStatusCodeException exception raised?

  1. When HttpStatusCodeException exception raised?

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

  2. When HttpStatusCodeException exception raised?

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

Solution 1

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

Original Author gtosto Of This Content

Solution 2

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.

Original Author vsk.rahul Of This Content

Solution 3

HTTP Status Codes are responses from the server, therefore if you have control of the server then you could make it return whichever errors you want. If you don’t have control of the server then you could try sending bad/invalid requests so that the server will complain.

Something like this on your server side:

@RequestMapping(method = RequestMethod.GET)
public ResponseEntity getAnError() {
    // complain!
    return ResponseEntity.status(HttpStatus.FORBIDDEN);
}

Original Author xtratic – Reinstate Monica Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment