Marshalling different classes using Spring MVC and JIBX

We are trying to create some RESTful services using Spring MVC. We will provide several views: XML, HTML and JSON. We would like to use JiBX as OXM technology.

We are currently struggling to figure out how to connect Spring using JiBX. If we want to connect one class, for example Customer, we simply define JibxMarshallerXML MarshallingViewand add it to ours as well ContentNegotiatingViewResolver. This works great.

The problem is that we are not sure how to include the sorting of several classes, for example, Customerand User. Each JibxMarshallercan support only one class (unlike Jaxb2Marshaller, which many can support). We tried to declare a marshaller for each class, but MarshallingViewonly one marshaller is supported. Multiple declaration MarshallingViewdoes not work (only the first file appears).

Your advice is welcome. Thank.

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

, targetClass JibxMarshaller. , , , , , . , , , , . :

<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>
+3
1

JiBX JiBX_bindingList . BindingFactory "targetClass" ( JiBX_bindingList). getMappedClasses() IBindingFactory, JibxMarshaller () , .

. JiBX.

+2

Source: https://habr.com/ru/post/1788364/


All Articles