I have a spring rest client. when authentication details are not provided in the headers and I hit the service with
ResponseEntity<String> resp = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
I get 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 hit the url on mozilla browser, I get message like
{"errors":[{"code":"xxxx","reason":"xxxx"}]}
Now here is what i want: i want to capture this error message in my code instead of getting 401 exception. I tried to see if there is anything in the response. But the response received is null. How can I capture the error message received from the webservice ?
I feel odd to post answer for my own question. But i finally achieved it through the following...
} catch (HttpClientErrorException e) {
System.out.println(e.getStatusCode());
System.out.println(e.getResponseBodyAsString());
}
I was incorrectly searching for errors in response entity. But it is available in the exception.
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