使用 Spring MVC 和 JIBX 编组不同的类

发布于 2024-10-14 09:44:09 字数 2687 浏览 4 评论 0 原文

我们正在尝试使用 Spring MVC 构建一些 RESTful 服务。我们将提供多种表示形式:XML、HTML 和 HTML。 JSON。我们想使用 JiBX 作为 OXM 技术。

我们目前很难弄清楚如何将 Spring 与 JiBX 连接起来。如果我们想要连接单个类,例如 Customer,我们只需定义一个 JibxMarshaller、一个 XML MarshallingView,并将其添加到我们的ContentNegotiatingViewResolver。这很好用。

问题是我们不确定如何连接多个类的编组,例如 CustomerUser。每个 JibxMarshaller 只能支持一个类(与可以支持多个类的 Jaxb2Marshaller 不同)。我们尝试为每个类声明一个编组器,但 MarshallingView 仅支持一个编组器。声明多个 MarshallingView 不起作用(看起来只有第一个起作用)。

感谢您的建议。谢谢。

<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 的回答进行更新:

事实证明,我被 JibxMarshallertargetClass 属性迷惑了。我认为这意味着编组器只能用于单个类,但是,它似乎只是使用目标类作为查找所有相关绑定的方式。因此,解决方案是仅使用单个编组器,使用您拥有绑定的类集中的任意目标类。例如:

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘亓 2024-10-21 09:44:09

JiBX 绑定编译器将 JiBX_bindingList 字段添加到类文件中。在运行时,“targetClass”(任何带有 JiBX_bindingList 字段的编译类)用于构建 BindingFactory。它是 getMappedClasses() >IBindingFactoryJibxMarshaller 使用
supports() 方法来检查编组器是否可以编组类。

另请参阅 JiBX 运行时用法

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文