Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring HttpClientErrorException provides no details from response body

Tags:

java

rest

spring

I'm updating legacy code that uses the exchange method of Spring 3.1 Framework's RestTemplate class. I came across what appears to be a major omission of detail. When the rest client with which I am attempting to communicate returns a 400 status code, a HttpClientErrorException is thrown but there is no response body to provide detail as to why the server rejected the request. There looks like there is no way to retrieve the response body which would provide very helpful information.

I do not need to figure out what is wrong in my calling code as I have already done that. I just would like to know if there is some way I could access and log the body of the HTTP response if an error occurs on the call.

like image 800
gogogadgetgeek Avatar asked Dec 11 '22 16:12

gogogadgetgeek


1 Answers

The response body is actually a property on HttpClientErrorException. It can be accessed via the following two accessors which it inherits from its parent class HttpStatusCodeException:

public byte[] getResponseBodyAsByteArray()
public String getResponseBodyAsString()
like image 180
gogogadgetgeek Avatar answered Feb 09 '23 00:02

gogogadgetgeek