java.net.SocketException:RestTemplate的参数无效

时间:2014-01-14 15:01:25

标签: java spring socketexception resttemplate

我正在使用RestTemplate来发送帖子请求。像这样:

restTemplate.postForObject(url, fetchImageDataRequest, SimpleImageHolder[].class);

当我像这样创建restTemplate时:

restTemplate = new RestTemplate();
MappingJacksonHttpMessageConverter converter = new MappingJacksonHttpMessageConverter();
restTemplate.getMessageConverters().add(converter);

一切正常。但是当我使用在spring.xml中创建的RestTemplate并且定义如下:

<bean id="reHttpConnectionManager" class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager">
  <property name="params">
    <bean class="org.apache.commons.httpclient.params.HttpConnectionManagerParams">
      <property name="soTimeout"                    value="30000"/>
      <property name="connectionTimeout"            value="30000"/>
      <property name="maxTotalConnections"          value="300"/>
      <property name="defaultMaxConnectionsPerHost" value="50"/>
    </bean>
  </property>
</bean>

<bean id="reRequestFactory" class="org.springframework.http.client.CommonsClientHttpRequestFactory">
  <property name="readTimeout" value="3000" />
  <constructor-arg>
    <bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
      <constructor-arg>
        <bean class="org.apache.commons.httpclient.params.HttpClientParams" />
      </constructor-arg>
      <property name="httpConnectionManager" ref="reHttpConnectionManager" />
      <property name="state">
        <bean class="org.apache.commons.httpclient.HttpState" />
      </property>
    </bean>
  </constructor-arg>
</bean>

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

我收到java.net.SocketException: Invalid argument个例外。试图解决这个问题。

完整的堆栈跟踪:

org.springframework.web.client.ResourceAccessException: I/O error: Invalid argument; nested exception is java.net.SocketException: Invalid argument
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:453)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:279)

1 个答案:

答案 0 :(得分:0)

我找不到问题所在,但我刚刚从bean HttpParams的构造函数中删除了httpClient bean并完全删除了HttpState,现在它可以工作了。新的XML看起来像这样:

<bean id="reHttpConnectionManager" class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager">
  <property name="params">
    <bean class="org.apache.commons.httpclient.params.HttpConnectionManagerParams">
      <property name="soTimeout"                    value="30000"/>
      <property name="connectionTimeout"            value="30000"/>
      <property name="maxTotalConnections"          value="300"/>
      <property name="defaultMaxConnectionsPerHost" value="50"/>
    </bean>
  </property>
</bean>

<bean id="reRequestFactory" class="org.springframework.http.client.CommonsClientHttpRequestFactory">
  <property name="readTimeout" value="3000" />
  <constructor-arg>
    <bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
      <property name="httpConnectionManager" ref="reHttpConnectionManager" />
    </bean>
  </constructor-arg>
</bean>

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