Use HttpResponse. getStatusLine() , which returns a StatusLine object containing the status code, protocol version and "reason".
HTTP Status Code 503 - Service Unavailable Another variety of the 500, a 503 response means that the server is unavailable.
Methods to Set HTTP Status Code Sr.No. This method sets an arbitrary status code. The setStatus method takes an int (the status code) as an argument. If your response includes a special status code and a document, be sure to call setStatus before actually returning any of the content with the PrintWriter.
RestClientException(java.lang.String pMessage) A constructor which takes a message, response object and error code. RestClientException(java.lang.String pMessage, java.lang.Throwable pCause) A constructor which takes a source exception, response object and error code.
Instead of catching RestClientException
, catch the special HttpClientErrorException
.
Here's an example:
try {
Link dataCenterLink = serviceInstance.getLink("dataCenter");
String dataCenterUrl = dataCenterLink.getHref();
DataCenterResource dataCenter =
restTemplate.getForObject(dataCenterUrl, DataCenterResource.class);
serviceInstance.setDataCenter(dataCenter);
} catch (HttpClientErrorException e) {
HttpStatus status = e.getStatusCode();
if (status != HttpStatus.NOT_FOUND) { throw e; }
}
HttpClientErrorException
provides getStatusCode
and getResponseBodyAsByteArray
to get the status code and body, respectively.
Catch RestClientResponseException
instead. It's more generic.
From the docs: Common base class for exceptions that contain actual HTTP response data.
In some cases, HttpClientErrorException is not thrown. For example the following method restTemplate.exchange call:
ResponseEntity<Employee[]> employees = restTemplate.exchange(url, HttpMethod.GET, entity, Employee[].class);
Gets the http body and marshalls it to an Entity. If remote resource returns a rare error, internal marshall does not work and just a RestClientException is thrown.
In this case or if you want to handle any error in restTemplate operations, you could use setErrorHandler. This method receives a basic ResponseErrorHandler with helpful methods.
This method hasError allowed me to get the remote http body text and helped me to detect the error of the invocation or in the remote http remote resource:
restTemplate.setErrorHandler(new ResponseErrorHandler() {
@Override
public boolean hasError(ClientHttpResponse arg0) throws IOException {
System.out.println("StatusCode from remote http resource:"+arg0.getStatusCode());
System.out.println("RawStatusCode from remote http resource:"+arg0.getRawStatusCode());
System.out.println("StatusText from remote http resource:"+arg0.getStatusText());
String body = new BufferedReader(new InputStreamReader(arg0.getBody()))
.lines().collect(Collectors.joining("\n"));
System.out.println("Error body from remote http resource:"+body);
return false;
}
@Override
public void handleError(ClientHttpResponse arg0) throws IOException {
// do something
}
});
Also, you can manually evaluate the body or status and return true or false in order to flag as error or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With