Question

I'm using RestTemplate in order to send a post request. Something like this:

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

When I create restTemplate like that:

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

Everything works fine. But when I use RestTemplate that is created in spring.xml and defined like this:

<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>

I get an java.net.SocketException: Invalid argument exception. Trying to figure this issue.

The full stacke trace:

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)
Was it helpful?

Solution

I couldn't find what the problem was but I just removed the HttpParams bean from the constructor of the bean httpClient and removed completely the HttpState and now it works. The new XML looks like this:

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top