Jaxb为Endpoint指定Marshaller

时间:2014-03-19 13:43:57

标签: xml spring jaxb moxy endpoint

我正在使用Spring框架。

我有多个mashallers和多个端点。我的marshallers的Jaxb上下文是用xml绑定文件定义的(使用eclipselink-oxm-xml jaxbContextProperties),因为我有多个编组任务。使用注释不是一个好的解决方案,因为同一个对象需要根据端点进行不同的编组/解组过程。

目前我对单个JaxbMarshaller和Endpoint的配置如下所示:

<sws:annotation-driven marshaller="marshallerA" unmarshaller="marshallerA"/>        

    <bean id="loggingInterceptor"
        class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor"/>


    <bean id="messageReceiver"
        class="org.springframework.ws.soap.server.SoapMessageDispatcher">
        <property name="endpointAdapters">
            <list>
                <ref bean="defaultMethodEndpointAdapter" />
            </list>
        </property>
    </bean> 

    <bean id="defaultMethodEndpointAdapter" 
        class="org.springframework.ws.server.endpoint.adapter.DefaultMethodEndpointAdapter">
        <property name="methodReturnValueHandlers">
            <list>
                <ref bean="marshallingPayloadMethodProcessor"/>
            </list>
        </property>
        <property name="methodArgumentResolvers">
            <list>
                <ref bean="marshallingPayloadMethodProcessor"/>
            </list>
        </property>     
    </bean>  


    <bean id="marshallingPayloadMethodProcessor"
        class="org.springframework.ws.server.endpoint.adapter.method.MarshallingPayloadMethodProcessor">
        <constructor-arg ref="marshallerA" />
        <constructor-arg ref="marshallerA" />
    </bean> 


    <bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping">
        <property name="order" value="1" />
    </bean>

    <sws:dynamic-wsdl id="myEndpointA"
        portTypeName="WebService"
        locationUri="/ws/myEndpointServiceA/"
        targetNamespace="http://company.com/schema/webServices" >

        <sws:xsd location="classpath:/path/to/schema/mySchemaA.xsd"/>

    </sws:dynamic-wsdl>

    <bean id="marshallerA" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="contextPath" value="x.y.z:x.y.w"/>
            <property name="jaxbContextProperties">
            <util:map>
                <entry key="eclipselink-oxm-xml">
                    <list>
                         <value>file:/bindingFileOne.xml</value>
                         <value>file:/bindingFileTwo.xml</value>
                         <value>file:/bindingFileThree.xml</value>    
                    </list>
                </entry>
            </util:map>
        </property>
    </bean>

现在说我有第二个端点和第二个marshaller,如何强制端点使用特定的编组?

感谢您的帮助! :)

2 个答案:

答案 0 :(得分:0)

我最终扩展了DefaultMethodEndpointAdapter,创建了SpecificEndpointAdapter。我只需要覆盖一个方法,实现如下:

public class SpecificEndpointAdapter extends DefaultMethodEndpointAdapter {
    private List<Object> acceptedEndpoints;

    public void setAcceptedEndpoints(List<Object> acceptedEndpoints) {
        this.acceptedEndpoints = acceptedEndpoints;
    }

    @Override
        protected boolean supportsInternal(MethodEndpoint methodEndpoint) {
        if(acceptedEndpoints.contains(methodEndpoint.getBean()))
            return super.supportsInternal(methodEndpoint);
        return false;
    }
}

在我的上下文文件中,这允许我将端点适配器限制为特定端点,因此具有特定的编组器:

<bean id="serviceAEndpointAdapter" class="my.package.SpecificEndpointAdapter">
    <property name="acceptedEndpoints">
        <list>
            <ref bean="serviceAEndpoint"/>
        </list>
    </property>
    <property name="methodReturnValueHandlers">
        <list>
            <ref bean="serviceA.marshallingPayloadMethodProcessor"/>
        </list>
    </property>
    <property name="methodArgumentResolvers">
        <list>
            <ref bean="serviceA.marshallingPayloadMethodProcessor"/>
        </list>
    </property>     
</bean>    

<bean id="serviceBEndpointAdapter" class="my.package.SpecificEndpointAdapter">
    <property name="acceptedEndpoints">
        <list>
            <ref bean="serviceBEndpoint"/>
        </list>
    </property>      
    <property name="methodReturnValueHandlers">
        <list>
            <ref bean="serviceB.marshallingPayloadMethodProcessor"/>
        </list>
    </property>
    <property name="methodArgumentResolvers">
        <list>
            <ref bean="serviceB.marshallingPayloadMethodProcessor"/>
        </list>
    </property>     
</bean>    

在我的MarhsallingPayloadProcessor中,我现在可以声明要使用哪个编组器。请注意,我最终使用bean id作为SpecificEndpointAdapter引用,因此如果您使用注释,则需要确定该端点的bean id。

我希望这有助于任何遇到同样问题的人,如果答案需要更多说明,请告诉我。

答案 1 :(得分:0)

只需澄清已接受答案的某些点,就可以使引用做Endpoint来接收端点,就像普通的bean一样:

@Bean
public SpecificEndpointAdapter csvEndpointAdapter(MyEndpoint myEndpoint) {

    List<MethodArgumentResolver> argumentResolvers =
            new ArrayList<>();
    argumentResolvers.add(methodProcessor());

    List<MethodReturnValueHandler> returnValueHandlers =
            new ArrayList<>();
    returnValueHandlers.add(methodProcessor());

    SpecificEndpointAdapter specificEndpointAdapter = new SpecificEndpointAdapter();
    specificEndpointAdapter.setMethodArgumentResolvers(argumentResolvers);
    specificEndpointAdapter.setMethodReturnValueHandlers(returnValueHandlers);
    specificEndpointAdapter.setAcceptedEndpoints(Arrays.asList(myEndpoint));

    return specificEndpointAdapter;
}

private MarshallingPayloadMethodProcessor methodProcessor() {
    return new MarshallingPayloadMethodProcessor(marshaller());
}

private Jaxb2Marshaller marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setContextPath("com.company.application");
    marshaller.setMtomEnabled(true);
    return marshaller;
}

MyEndpoint

@Endpoint
public class MyEndpoint {
   //...
}