How to add message converter to restTemplate in xml configuration

Arpan Das picture Arpan Das · Jun 5, 2015 · Viewed 11k times · Source

I am using restTemplate for consuming json data from remote server, and then parsing the json in my java objects using jackson. I have added required message converters to my restTemplate by java code as:

List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new FormHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new MappingJacksonHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
restTemplate.setRequestFactory(new CommonsClientHttpRequestFactory());

MyResponse myResponse = restTemplate.getForObject(caasUrl, MyResponse.class);

And my restTemplate is defined in my spring-config file as:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
        <constructor-arg>
            <bean class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
                <property name="readTimeout" value="630000" />
                <property name="connectTimeout" value="30000" />
            </bean>
        </constructor-arg>
    </bean>

And its working fine, now my question is how can we pass the list of message converters to restTemplate in xml configuration as I do not want to do the same by java code. any Help please

Answer

Sheetal Mohan Sharma picture Sheetal Mohan Sharma · Jun 5, 2015

Try this...sample code below shows only 2 converters.

<property name="messageConverters">
    <list>
        <bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
        ......more....
    </list>
</property>