[英]Spring RestTemplate handle custom status code
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: 我应该从我的应用程序中调用一些服务,该服务可以返回异常的http状态代码,例如230、240等。默认情况下,我得到以下错误处理程序:
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
: 但是,当RestTmplate尝试检索它时,它会陷入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? 如何正确获得反应身体?
最后,在Spring 5
解决了问题, 对其进行了测试 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.