Spring RestTemplate - no suitable HttpMessageConverter found for response type [..] and content type [application/xml]

StackOverflow https://stackoverflow.com/questions/8802499

Question

I have a problem accessing a REST service with the RestTemplate. I've already managed to user the MarshallingConverter to access one other service, and everything worked fine. I have copied this functionality and generated Model classes from the XSD schema I've received. However I get an exception that no suitable converter was found. Here is my configuration (I am using Spring 3.0.6 in connection with Vaadin if that matters):

<bean id="marshallingConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
    <property name="marshaller" ref="jaxbMarshaller" />
    <property name="unmarshaller" ref="jaxbMarshaller" />
</bean>

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            ... here are my model classes ...
        </list>
    </property>
</bean>

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <property name="messageConverters">
        <list>
            <ref bean="marshallingConverter" />
        </list>
    </property>
</bean>

The RestTemplate is autowired in my service client class implementation. What am I missing here? I have checked the response and the content type is set to application/xml and the model classes were autogenerated, so the configuration should be right. Thanks for any help.

Was it helpful?

Solution

So I've made this work. The part of the problem was, that the generated classes were missing XmlRootElement annotation. More about it here. So I've added the XJC annotation to the XSD schema and generated the classes again with:

xjc -extension /path/to/schema

After that everything has worked(or at least I didn't get any exception processing the XML), but the elements from inside the root element were not unmarshallised and I got only the XMLElement implpementation from the parser. The problem was (this was specific for my project), that the element definition used xs:anyType as the element type, which cannot be parsed. After I've changed it to the right element type, everything worked like charm.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top