如何从Java异常获取responseBody
调用REST服务时,它给出了如何从Java异常获取responseBody,java,exception,httpresponse,Java,Exception,Httpresponse,调用REST服务时,它给出了HttpClientErrorException,我能够检索状态代码和错误消息,但是如何获取响应库 我正在尝试下面的代码,但无法键入到HttpResponse catch(HttpClientErrorException e) { // I am trying to typecast to HttpResponse, but its throwing error HttpResponse response = (HttpResponse) e;
HttpClientErrorException
,我能够检索状态代码和错误消息,但是如何获取响应库
我正在尝试下面的代码,但无法键入到HttpResponse
catch(HttpClientErrorException e) {
// I am trying to typecast to HttpResponse, but its throwing error
HttpResponse response = (HttpResponse) e;
String msg = response.getEntity().getContent().toString();
}
我做错了什么?有人可以提出建议吗?
HttpClientErrorException
扩展了RestClientResponseException
,其中包含方法getResponseBodyAsString()
因此,将其强制转换为HttpResponse
是错误的,事实上HttpClientErrorException
并不扩展HttpResponse
就这么做吧
catch(HttpClientErrorException e){
String body = e.getResponseBodyAsString();
}
有关更多信息您是否查阅了javadocs中的HttpClientErrorException?您将看到它继承了这些方法
getResponseBodyAsByteArray、getresponsebodyastring、getResponseHeaders
-它们不合适吗?谢谢。如果您接受答案,我将不胜感激,如果有用,谢谢!