简体   繁体   English

Spring Rest模板-204内容来自Web服务

[英]Spring Rest template - 204 content in response from webservice

I have a rest service and is consumed using Spring's RestTemplate with Apache HttpClient as, 我有一个休息服务,并且使用Spring的RestTemplate和Apache HttpClient来使用,

    @Autowired
    public ClientImpl(@Value("${base-uri}") final String baseUrl,
                              @Qualifier("restOperations") RestOperations restTemplate) {
        serviceUrl = baseUrl;
        restTemplate = restTemplate;
    }

   private List<ResponseDetails> processRequest(CustomRequest request) throws Exception {
    ResponseEntity<ResponseDetails[]> responseEntity = restTemplate.exchange(serviceUrl, HttpMethod.POST, entity, ResponseDetails[].class);
    if (responseEntity.getStatusCode().value() == 204) {
        return Collections.<ResponseDetails>emptyList();
    }
    ResponseDetails[] response = responseEntity.getBody();
    return response != null ? Lists.newArrayList(response) : Collections.<ResponseDetails>emptyList();
}

When the webservice returns 204 response, then the second service call after 204 response, fails with read timeout. 当Web服务返回204响应时,则204响应之后的第二个服务调用会因读取超时而失败。

Spring-web : 4.3.5 春季网:4.3.5

I cannot figure out the cause. 我不知道原因。 Any help? 有什么帮助吗?

EDIT: From debug logs, 编辑:从调试日志,

org.apache.http.impl.conn.DefaultHttpResponseParser;Garbage in response: ÿþ{"id":0}HTTP/1.1 204 Could not find org.apache.http.impl.conn.DefaultHttpResponseParser;响应中的垃圾:ÿþ{“ id”:0} HTTP / 1.1 204找不到

Response in server logs by httpclient: httpclient在服务器日志中的响应:

<204 No Content,{Cache-Control=[no-cache], Pragma=[no-cache], Content-Type=[application/json; <204没有内容,{Cache-Control = [no-cache],Pragma = [no-cache],Content-Type = [application / json; charset=utf-16], Expires=[-1], Server=[some], X-AspNet-Version=[someversion], X-Powered-By=[ASP.NET], Date=[somedate]}> charset = utf-16],Expires = [-1],Server = [some],X-AspNet-Version = [someversion],X-Powered-By = [ASP.NET],Date = [somedate]}>

HTTP 204 is status code for "No Content", but there seems to be garbage content in the response. HTTP 204是“无内容”的状态代码,但是响应中似乎有垃圾内容。 This can be seen on your logs: 这可以在您的日志中看到:

ÿþ{"id":0}

This is the cause of problems you have. 这是您遇到问题的原因。

HTTP client is not expecting anything in the body content of 204 response so does not read it, hence response handler does not see that there is any garbage. HTTP客户端不希望204响应的正文内容中有任何内容,因此不会读取它,因此响应处理程序不会看到有任何垃圾。 However since there is garbage which is not yet consumed the connection stays open until it is read -> the next connection, which tries to reuse the connection, gets hit with a read timeout. 但是,由于存在尚未消耗的垃圾,因此连接将保持打开状态直到被读取->试图重用该连接的下一个连接会因读取超时而命中。

There is a separate thread about a similar problem , where the problem is worked around with a custom HTTP request executor. 关于类似问题 ,有一个单独的线程 ,该问题可通过自定义HTTP请求执行程序解决。 Using such executor, you could call getBody() to obtain the garbage response body and then next request would not have any issues. 使用这样的执行程序,您可以调用getBody()来获取垃圾响应正文,然后下一个请求不会有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM