0

sorry to asking this kind of question. I am attempting to handle exception when calling rest services. I have to throw back to web application if time out error or other rest client exceptions in my rest web service while connecting / throwing external web service.

I am trying to handle by like this.

try {

    final RestTemplate restTemplate = new RestTemplate();

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity<String> entity = new HttpEntity<String>(reqJson, headers);

    String respJson= restTemplate.postForObject(uri, entity, String.class);

} catch (HttpStatusCodeException e) {
    System.out.println(" Response Status Code "+e.getStatusCode().value());
    throw e;
}

But i am not able to catch the status code when time out error occurring.

Then what is the use of HttpStatusCodeException, when should i use this.

I need to throw different kind of exception like 400, 404, 500, 408 and all to web application. Please advice.

deadend
  • 1,286
  • 6
  • 31
  • 52
  • 1
    If you cannot connect to the remote server, their is no status code... Just catch the base exception, check if it is a `HttpStatusCodeException` return a status, else create a status. yourself. – M. Deinum May 11 '17 at 11:03
  • @ M. Deinum. then what is the purpose to catch HttpStatusCodeException in catch block. because we dont know about external service. – deadend May 11 '17 at 11:10
  • To extract the code and return it to your client (instead of an exception which will always return in a error 500 because it is an exception). – M. Deinum May 11 '17 at 11:17
  • 1
    @ M. Deinum, sorry to ask again. can you please brief in detail. i have a client and i am a client to external web service. in this case what is the need of catching HttpStatusCodeException and how should i return staus code to my client. – deadend May 11 '17 at 11:19

0 Answers0