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

  • RestTemplate / JAXB issue

    Hi
    I am using RestTemplate and JAXB as a message converter. (org.springframework.oxm.jaxb.Jaxb2Marshaller)
    I am calling:
    Code:
    		// 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.

    Code:
    		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?


    Thanks

    Avishay

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

    Final Year IEEE Projects

    Comment


    • #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.

      Mark

      Comment


      • #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:

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

        <bean id="restTemplate" class="org.springframework.web.client.RestTemplate ">
        <constructor-arg>
        <bean class="org.springframework.http.client.HttpCompone ntsClientHttpRequestFactory">
        <constructor-arg ref="httpClient" />
        </bean>
        </constructor-arg>
        <!-- Configure the Rest template to translate between XML and JAXB -->
        <property name="messageConverters">
        <list>
        <bean class="org.springframework.http.converter.xml.Mars hallingHttpMessageConverter">
        <property name="marshaller" ref="jaxbMarshaller" />
        <property name="unmarshaller" ref="jaxbMarshaller" />
        </bean>
        <bean class="org.springframework.http.converter.StringHt tpMessageConverter"/>
        </list>
        </property>
        </bean>
        <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">
        <list>
        <value>com.myorg.path.to.JAXB.classes</value>
        </list>
        </property>
        </bean>
        </beans>

        Comment

        Working...
        X