2017-04-24 29 views
0

在调用REST服务时,它给出HttpClientErrorException,我能够检索状态码和错误消息,但是如何获得responseBody如何从Java异常获得响应身体异常

我在尝试波纹管代码,但无法调整到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(); 
} 

我在做什么错?任何人都可以请建议?

+1

你查找的Javadoc HttpClientErrorException?你会看到它继承了这些方法:getResponseBodyAsByteArray,getResponseBodyAsString,getResponseHeaders - 它们不适合吗? –

回答

1

HttpClientErrorException延伸RestClientResponseException它包含方法getResponseBodyAsString()

所以这是它转换为HttpResponse一个错误,其实HttpClientErrorException不延长HttpResponse

只要做到这一点

catch(HttpClientErrorException e){ 
    String body = e.getResponseBodyAsString(); 
} 

欲了解更多信息http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/HttpClientErrorException.html

+0

谢谢Flood2d。 – Bharath

+0

如果您接受答案,我将不胜感激,如果有用,谢谢! – Flood2d

+1

完成,谢谢:-) – Bharath