【问题标题】:Two different marshallers in spring project春季项目中的两个不同编组器
【发布时间】:2011-01-19 14:53:48
【问题描述】:

我需要使用两个不同的编组器,通常是 JaxbMarshaller 和 CastorMarshaller。 我有很多集成模块的 spring 项目。

<bean id="marshaller" class="org.springframework.oxm.castor.CastorMarshaller">
    <property name="mappingLocation">
        <value>classpath:config/service/mapping.xml</value>
    </property>
</bean>
<bean
    class="org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter">
    <property name="marshaller" ref="marshaller" />
    <property name="unmarshaller" ref="marshaller" />
</bean>

我还将它添加到我的端点以提供 JaxbMarshaller,但它没有得到它

public class MyEndPoint extends AbstractMarshallingPayloadEndpoint

我需要同时使用 JaxbMarshaller 和 CastorMarshaller

【问题讨论】:

    标签: jaxb jax-ws marshalling spring-ws castor


    【解决方案1】:

    最终有两个问题需要解决:

    1. 同时注入 JAXB 和 Castor 编组器/解组器
    2. 确定何时使用 JAXB 或 Castor

    项目 #1 - 同时注入 JAXB 和 Castor 编组器/解组器

    org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter 只有一个 marshaller 属性和一个 unmarshaller 属性。解决这个问题可能有两种方法:

    选项 #1 - 子类 GenericMarshallingMethodEndpointAdapter

    您可以继承 rg.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter 并引入第二个 marshaller 和 unmarshaller 属性。然后您将配置如下:

    <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="contextPath" value="com.example"/>
    </bean>
    <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller">
        <property name="mappingLocation">
            <value>classpath:config/service/mapping.xml</value>
        </property>
    </bean>
    <bean
        class="your.domain.YourGenericMarshallingMethodEndpointAdapter">
        <property name="marshaller" ref="jaxbMarshaller" />
        <property name="unmarshaller" ref="jaxbMarshaller" />
        <property name="castorMarshaller" ref="castorMarshaller" />
        <property name="castorMarshaller" ref="castorMarshaller" />
    </bean>
    

    选项 #2 - 实现您自己的 Marshaller

    您可以实现自己的同时支持 JAXB 和 Castor 的编组器。然后配置如下:

    <bean id="marshaller" class="your.domain.CustomMarshaller">
        <property name="contextPath" value="com.example"/>
        <property name="mappingLocation">
            <value>classpath:config/service/mapping.xml</value>
        </property>
    </bean>
    <bean
        class="org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter">
        <property name="marshaller" ref="marshaller" />
        <property name="unmarshaller" ref="marshaller" />
    </bean>
    

    第 2 项 - 确定何时使用 JAXB 或 Castor

    这可能是更难解决的问题。一旦您的端点知道 JAXB 和 Castor,您仍然需要选择一个来执行编组操作。使用上述自定义编组器方法可能更容易解决这个问题。

    更多信息

    以下是使用 Spring 配置 JAXB 的说明:

    以下包含配置 Castor(和 JAXB)的说明:

    【讨论】:

    • 感谢您的关注。但我需要在同一个项目中同时使用它们
    • 你试过在配置中组合它们吗?我已经更新了我的答案,以包括这可能看起来像的片段。
    • 我已经编辑了这个答案的描述我在终点做了一些改变
    • 你能尝试我建议的方法吗?您需要将 JAXB 和 Castor 编组器绑定到不同的属性。
    • 其实我没有。有了 GenericMarshallingMethodEndpointAdapter,我现在如何使用它们。这与网络服务无关。如果您不想使用 PayLoad EndPoint 编组对象,这可能很有用
    【解决方案2】:

    我以死胡同结束了,我省略了 JAXB,我只使用 Castor。

    【讨论】:

      猜你喜欢
      • 2013-06-27
      • 1970-01-01
      • 2011-04-24
      • 2020-09-07
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多