RestTemplate Bad请求许多请求

时间:2016-03-06 15:32:46

标签: spring httpclient apache-httpclient-4.x resttemplate

我遇到了休息模板交换方法的问题。我用它将请求发送到谷歌的GCM服务器。每次相同的请求,只有收件人不同(JSON中的字段)。当我同时发送超过200个请求时,在207我得到400 Bad Request响应,之后我可以等待一段时间但仍然从我的服务器获得相同的响应。看起来我的代码阻止了对网络资源的访问。当我重新启动服务器时,资源再次可用。 RestTemplate配置:

<bean id="restTemplate"                 class="org.springframework.web.client.RestTemplate">
    <constructor-arg ref="httpRequestFactory"> </constructor-arg>
</bean>

<bean id="httpRequestFactory"           class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
    <constructor-arg ref="httpClient"> </constructor-arg>
    <property name="connectTimeout" value="1000" />
</bean>


<bean id="httpConnectionManager"        class="org.apache.http.impl.conn.PoolingClientConnectionManager">
     <property name="defaultMaxPerRoute" value="50" />
        <property name="maxTotal" value="100" />
</bean>


<bean id="httpClient" class="org.apache.http.impl.client.DefaultHttpClient" >
     <constructor-arg ref="httpConnectionManager" />
</bean>

请求:

ResponseEntity<String> response = restTemplate.exchange(
            "myaddress",
            HttpMethod.POST, request, String.class);

日志: DEBUG [org.springframework.web.client.RestTemplate](gcmjmsContainer-1)为&#34; https://gcm-http.googleapis.com/gcm/send&#34;

创建了POST请求
DEBUG [org.springframework.web.client.RestTemplate] (gcmjmsContainer-1) Setting request Accept header to [text/plain, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, application/*+json, */*]

DEBUG [org.springframework.web.client.RestTemplate] (gcmjmsContainer-1) Writing [{
  "to" : "1",
  "notification" : {
    "text" : "mytext",
    "click_action" : "SHOW"
  },
  "data" : {
    "orderid" : 1,
    "status" : "paid"
  }
}] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@1d92cbc8]

DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] (gcmjmsContainer-1) Connection request: [route: {s}->https://gcm-http.googleapis.com:443][total kept alive: 2; route allocated: 1 of 50; total allocated: 2 of 100]

DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] (gcmjmsContainer-1) Connection leased: [id: 1][route: {s}->https://gcm-http.googleapis.com:443][total kept alive: 1; route allocated: 1 of 50; total allocated: 2 of 100]

DEBUG [org.apache.http.impl.client.DefaultHttpClient] (gcmjmsContainer-1) Stale connection check

DEBUG [org.apache.http.impl.client.DefaultHttpClient] (gcmjmsContainer-1) Attempt 1 to execute request

DEBUG [org.apache.http.impl.conn.DefaultClientConnection] (gcmjmsContainer-1) Sending request: POST /gcm/send HTTP/1.1

DEBUG [org.apache.http.impl.conn.DefaultClientConnection] (gcmjmsContainer-1) Receiving response: HTTP/1.1 400 Bad Request

DEBUG [org.apache.http.impl.client.DefaultHttpClient] (gcmjmsContainer-1) Connection can be kept alive indefinitely

WARN  [org.springframework.web.client.RestTemplate] (gcmjmsContainer-1) POST request for "https://gcm-http.googleapis.com/gcm/send" resulted in 400 (Bad Request); invoking error handlerng error handler

1 个答案:

答案 0 :(得分:1)

实际上我已经找到解决方案了。我想到在Accept标头中有许多条目“application / json”。我在代码中的错误是:     restTemplate.getMessageConverters()。添加(                 new MappingJackson2HttpMessageConverter());

每次我使用单例restTemplate发送请求时执行