我们在我们的应用程序中有两个部分:服务器 – 提供REST服务和客户端 – 通过
Spring restTemplate消耗它们.
除了HTTP状态之外,我们的服务器还会返回具有描述错误的JSON的HTTP主体.
所以,我已经将restim错误处理程序添加到restTemplate,以将一些错误编码为非错误 – 它有助于解析HTTP身体.
但是在HTTP / 1.1 401未经授权的情况下,通过解析HTTP主体可以获得异常.
所有其他错误代码处理正常(400,402等)
我们使用普通的服务器逻辑,发送错误时发送HTTP响应,对于不同类型的错误没有特殊规则:
writeErrorToResponse(int status, String errMsg, HttpServletResponse resp) throws IOException {
response.setStatus(status);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
String message = String.format("{"error":"%s"}", StringUtils.escapeJson(errMsg));
resp.getWriter().println(message);
}
但是在客户端只有HTTP / 1.1 401抛出异常 – “java.net.HttpRetryException:由于服务器身份验证,在流式传输模式下无法重试”
我做了一些调试,看到问题的原因是SimpleClientHttpResponse中的代码:
HttpURLConnection.getInputStream()
跟Fiddler跟踪我有下一个回应:
消息在客户端被解析正确:
HTTP/1.1 402 Payment Required
X-Powered-By: Servlet/3.0
Content-Type: application/json
Content-Language: en-GB
Content-Length: 55
Connection: Close
Date: Sat, 25 May 2013 10:10:44 GMT
Server: WebSphere Application Server/8.0
{"error":"I cant find that user. Please try again."}
和消息是异常的原因:
HTTP/1.1 401 Unauthorized
X-Powered-By: Servlet/3.0
Content-Type: application/json
Content-Language: en-GB
Content-Length: 55
Date: Sat, 25 May 2013 11:00:21 GMT
Server: WebSphere Application Server/8.0
{"error":"I cant find that user. Please try again."}
我的问题是在这种情况下可能是java.net.HttpRetryException的原因?
另外:有一段时间以前这种机制很好.但是由于我们已经改变了很多应用程序中的代码.



