3

We are trying to build some RESTful services with Spring MVC. We will be providing several representations: XML, HTML, & JSON. We would like to use JiBX as the OXM technology.

We are currently having difficulty figuring out how to wire up Spring with JiBX. If we want to wire up a single class, for example Customer, we simply define a JibxMarshaller, an XML MarshallingView, and add it too our ContentNegotiatingViewResolver. This works great.

The problem is we aren't sure how to wire up marshalling of multiple classes, for example, Customer and User. Each JibxMarshaller can support only one class (unlike the Jaxb2Marshaller which can support many). We tried declaring a marshaller for each class, but the MarshallingView only support one marshaller. Declaring multiple MarshallingViews does not work (it appears only the first one works).

Your advice is appreciated. Thanks.

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <util:map>
            <entry key="xml" value="application/xml"/>
        </util:map>
    </property>
    <property name="defaultViews">
        <util:list>
            <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                <property name="marshaller" ref="userMarshaller"/>
            </bean>
            <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                <property name="marshaller" ref="customerMarshaller"/>
            </bean>
        </util:list>
    </property>
</bean>

<bean id="userMarshaller" class="org.springframework.oxm.jibx.JibxMarshaller">
    <property name="targetClass" value="com.mycompany.User"/>
</bean>
<bean id="customerMarshaller" class="org.springframework.oxm.jibx.JibxMarshaller">
    <property name="targetClass" value="com.mycompany.Customer"/>
</bean>

Update based on Ritesh's answer below:

It turns out that I was thrown off by the targetClass property of the JibxMarshaller. I thought it meant the marshaller would only work for a single class, however, it appears to just uses the target class as a way of finding all related bindings. So, the solution is to use just a single marshaller, using an arbitrary target class from your set of classes you have bindings for. For example:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <util:map>
            <entry key="xml" value="application/xml"/>
        </util:map>
    </property>
    <property name="defaultViews">
        <util:list>
            <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                <property name="marshaller" ref="jibxMarshaller"/>
            </bean>
        </util:list>
    </property>
</bean>

<bean id="jibxMarshaller" class="org.springframework.oxm.jibx.JibxMarshaller">
    <property name="targetClass" value="com.mycompany.User"/>
</bean>
SingleShot
  • 18,821
  • 13
  • 71
  • 101

1 Answers1

2

JiBX binding compiler adds JiBX_bindingList field to class files. At run time, the 'targetClass' (any compiled class with JiBX_bindingList field) is used to build a BindingFactory. It is the getMappedClasses() of IBindingFactory which is used by JibxMarshaller in supports() method to check if marshaller can marshal a class.

Please also see JiBX runtime usage.

Ritesh
  • 7,472
  • 2
  • 39
  • 43
  • I'm finally revisiting this after several weeks and upon further investigation, it appears you are correct. I think the misunderstanding on my part is the "targetClass " property on the marshaller. To me, this property implies the marshaller will only work for the target class, which is not true. – SingleShot Feb 22 '11 at 19:47
  • I've appended my solution to the original question. Thanks for your help! – SingleShot Feb 22 '11 at 21:43
  • I have this exact same problem but giving a single jibx marshler with an arbitrary class has not solved my problem. Is there any thing else which i should do? – Richie May 15 '13 at 07:53
  • I haven't used it for some time and so not sure if anything got changed. If you have a decompiler, can you verify that the (jibx compiled) class that you selected has JiBX_bindingList field? – Ritesh May 15 '13 at 13:03