获取RestTemplate客户端从服务器抛出的异常的描述性自定义消息

时间:2013-12-13 05:14:44

标签: java spring spring-mvc resttemplate

我正在使用Spring 3.0.5 Rest Full WebService。

在客户端使用RestTemplate所以当服务器抛出任何非HttpStatus.OK响应状态的异常时我的问题。

然后我希望通过Spring RestTemplate在客户端的服务器端的异常消息。目前我得到的所有时间相同的异常(ResourceAccessException)带有一些通用消息,例如得到的以下是方法失败状态的例外:

org.springframework.web.client.ResourceAccessException: I/O error: Server returned HTTP response code: 420 for URL: https://<URL>:<Port>/<Server Application Name>/?param=12345; nested exception is java.io.IOException: Server returned HTTP response code: 420 for URL: <URL>:<Port>/<Server Application Name>/?param=12345

所以当时我想要从RestTemplate客户端

的服务器抛出异常的完整描述性自定义消息

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

我已经回答了类似的问题here。当您在使用rest模板时从服务获得非2xx响应代码时,您需要实现ResponseErrorHandler以拦截响应代码,正文和标头。复制所需的所有信息,将其附加到自定义异常并将其丢弃以供客户端使用。您可以在上面的链接中看到代码。