Announcement Announcement Module
No announcement yet.
RestTemplate / JAXB issue Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • RestTemplate / JAXB issue

    I am using RestTemplate and JAXB as a message converter. (org.springframework.oxm.jaxb.Jaxb2Marshaller)
    I am calling:
    		// I have a POJO named API which is generated by xjc from a xsd
                    // RHEVM_API  is a URL
                   API api =  restTemplate.getForObject(RHEVM_API, API.class);
    As a result I get a ClassCastException telling that I try to cast JAXBElement to API.
    After a debug session I understood that the 'getForObject' call return a JAXBElement which contains the API POJO inside. Calling the getValue() method of this JAXBelement return the API POJO.

    		JAXBElement<?> jaxbElement = restTemplate.getForObject(RHEVM_API, JAXBElement.class);
    		return (API)jaxbElement.getValue();
    Looking at RestTemplate examples I see that no one has to do this 'trick'.
    I know I am missing something here :-(
    What is it?



  • #2
    I also facing the same prob...

    Final Year IEEE Projects


    • #3
      Just out of curiosity. I thought the RestTemplate gives you a default HttpMessageConverter, that already know how to convert using JAXB, where all you needed was the JAXB jars on the classpath, and your entity to have JAXB mapping annotations and then everything just worked automatically.

      That is probably what you see in the examples. They aren't changing out the HttpMessageConverter that the Template by default uses.

      Try it out. I could be completely wrong, as I have very little experience with the RestTemplate.



      • #4
        The RestTemplate needs to be configured so that is supports JAXB, and JAXBElement in particular. By default it supports neither

        To support JAXB, you will need to wire "Jaxb2Marshaller" into RestTemplate. Rather infuriatingly, Jaxb2Marshaller has a boolean property supportJaxbElementClass that is false by default. You will want to set that to true!

        The following example configuration shows how this can be done:

        <bean id="httpClient" class="org.apache.http.impl.client.DefaultHttpClie nt">
        <bean class="org.apache.http.impl.conn.PoolingClientConn ectionManager" />

        <bean id="restTemplate" class="org.springframework.web.client.RestTemplate ">
        <bean class="org.springframework.http.client.HttpCompone ntsClientHttpRequestFactory">
        <constructor-arg ref="httpClient" />
        <!-- Configure the Rest template to translate between XML and JAXB -->
        <property name="messageConverters">
        <bean class="org.springframework.http.converter.xml.Mars hallingHttpMessageConverter">
        <property name="marshaller" ref="jaxbMarshaller" />
        <property name="unmarshaller" ref="jaxbMarshaller" />
        <bean class="org.springframework.http.converter.StringHt tpMessageConverter"/>
        <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshalle r">
        <!-- In particular, support JAXBElement<T> where T is a class defined in packagesToScan -->
        <property name="supportJaxbElementClass" value = "true"/>
        <property name="packagesToScan">