在调用 REST 服务时,它给出了 HttpClientErrorException
,我能够检索状态代码和错误消息,但我如何才能获得 responseBody
?
我正在尝试下面的代码,但无法转换为 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();
}
关于java - 如何从 Java Exception 中获取 responseBody,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43578618/