2

I have written some consument of REST using spring boot. It is working for me.

However, I would like to be able to get the body of response even in the case of Bad request. At this moment it throws an exception, however, I want to read error of message.

To be more precise:
Webservice always returns JSON, something like that:

{
  errorCode : 12,
  errorMessage: "Can't find element"
}

or

{
   errorCode : 0,
   errorMessage: ""
}

I have the corresponding model in my code:

class Response {
  int errorCode;
  String errorMessage;
}    
Request req = new Request();
Response response = restTemplate.postForObject(SOME_ENDPOINT, request,Response.class);

However, in the case of BadRequest, I can't get an error message (so response object) because it throws exception RestClientException.

How to suppress this exception and get a response ?

Arghavan
  • 1,125
  • 1
  • 11
  • 17

0 Answers0