I should call some service from my application that can return unusual http status codes, such as 230, 240 etc. By default error handler I'm getting:
Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.UnknownHttpStatusCodeException: Unknown status code [230] null] with root cause...
When I use my custom error handler I can avoid this:
@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
int status = response.getRawStatusCode();
if (status >= 200 && status <= 299)
return false;
HttpStatus statusCode = response.getStatusCode();
if (statusCode.is2xxSuccessful())
return false;
HttpStatus.Series series = statusCode.series();
return (HttpStatus.Series.CLIENT_ERROR.equals(series)
|| HttpStatus.Series.SERVER_ERROR.equals(series));
}
But when RestTmplate tries to retrieve it falls into the same exception in MessageBodyClientHttpResponseWrapper
:
public boolean hasMessageBody() throws IOException {
HttpStatus responseStatus = this.getStatusCode();
if (responseStatus.is1xxInformational() || responseStatus == HttpStatus.NO_CONTENT ||
responseStatus == HttpStatus.NOT_MODIFIED) {
return false;
}
else if (this.getHeaders().getContentLength() == 0) {
return false;
}
return true;
}
How can I get response body correctly?
HttpStatus
is an enumeration, and there is no support for unassigned HTML codes. You have two options, basically: 1. overwrite all offending code (use your own implementations that don't call to that enumeration, only using raw status code) on the path of processing those responses; 2. request enhancement from Spring team (there might already be one). – M. Prokhorov Aug 23 '17 at 13:30