【发布时间】:2011-01-28 00:38:58
【问题描述】:
我们正在尝试使用 Spring MVC 构建一些 RESTful 服务。我们将提供几种表示形式:XML、HTML 和 JSON。我们想使用 JiBX 作为 OXM 技术。
我们目前很难弄清楚如何将 Spring 与 JiBX 连接起来。如果我们想连接一个类,例如Customer,我们只需定义一个JibxMarshaller、一个XML MarshallingView,并将它也添加到我们的ContentNegotiatingViewResolver。这很好用。
问题是我们不确定如何连接多个类的编组,例如Customer 和User。每个JibxMarshaller 只能支持一个类(与可以支持多个类的 Jaxb2Marshaller 不同)。我们尝试为每个类声明一个编组器,但MarshallingView 只支持一个编组器。声明多个 MarshallingViews 不起作用(似乎只有第一个起作用)。
感谢您的建议。谢谢。
<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>
根据以下 Ritesh 的回答进行更新:
原来我被JibxMarshaller 的targetClass 属性甩了。我认为这意味着编组器仅适用于单个类,但是,它似乎只是使用目标类作为查找所有相关绑定的一种方式。因此,解决方案是只使用一个编组器,使用您绑定的一组类中的任意目标类。例如:
<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>
【问题讨论】:
标签: java spring spring-mvc jibx spring-oxm