Spring Rest Client wants to see error message instead of exception

I have a spring rest client. when authentication data is not specified in headers and I hit the service with ResponseEntity<String> resp = restTemplate.exchange(url, HttpMethod.GET, request, String.class);

I get an exception invoking error handler Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 401 Unauthorized at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)

But when I find the url in the mozilla browser I get a message like {"errors":[{"code":"xxxx","reason":"xxxx"}]}

Now this is what I want: I want to capture this error message in my code instead of getting a 401 exception . I tried to figure out if there is anything in the answer. But the answer received is zero. How can I capture error message received from webservice

+3


source to share


1 answer


It seems strange to me to post the answer to my question. But I finally achieved this through the following ...

} catch (HttpClientErrorException e) {
      System.out.println(e.getStatusCode());
      System.out.println(e.getResponseBodyAsString());
    }

      



I was looking wrongly for errors in the answer object. But it is available in an exception.

+23


source







All Articles