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 ????????
exhange(...) method of
org.springframework.web.client.RestTemplate
class was added in3.1.0.RELEASE
of spring-web library.This method throws
RestClientException
which covers client (4_xx) and server (5_xx) side http code errors. ButRestClientException
doesn't offergetStatusCode(), getResponseAsString()
, etc... methods.HttpsStatusCodeException
is the child ofRestClientException
which is doing same thing but with additional methods likegetStatusCode(), getResponseAsString()
, etc.HttpClientErrorException
child ofHttpsStatusCodeException
and only entertain client error (4_xx) not the server error.HttpServerErrorException
child ofHttpsStatusCodeException
and only entertain server error (5_xx) not the client error.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:
According to documentation there are two types of
HttpStatusCodeException
HttpClientErrorException
andHttpServerErrorException
.so you can just use ResponseEntity.BodyBuilder.status(505) for example to raise an
HttpServerErrorException
in your