将spring-ws实现转换为spring-integration-ws实现

时间:2012-08-02 11:00:32

标签: spring-ws spring-integration spring-jms

我使用spring-ws实现了Web服务(服务器端)。我想更改我的代码以使用spring-integration-ws(ws:inbound-gateway和channels)。我已尝试过示例,但仍不确定是否可以转换我的实现。简而言之,我使用@ Endpoint,@ PayRoot,PayloadRootAnnotationMethodEndpointMapping和JMS(作为传输)。请在下面找到我的实施:

端点类(MyEndpoint.java):

@Endpoint
public class MyEndpoint {
    @PayloadRoot(localPart="getUserRequest", namespace="http://play.zahid.springint/Jibx")
    public GetUserResponse getUserResponse(GetUserRequest request){
        log.info("Start getUserResponse " + request);
        GetUserResponse response = new GetUserResponse();

        User user = new User();
        user.setFirstName(request.getFirstName());
        user.setLastName(request.getFirstName() + " Last");
        response.setUser(user);

        log.info("End getUserResponse " + response);
        return response;
    }

    @PayloadRoot(localPart="sayHiUserRequest", namespace="http://play.zahid.springint/Jibx")
    public SayHiResponse sayHiUserResponse(SayHiUserRequest request){
        log.info("Start sayHiUserResponse " + request);
        SayHiUserResponse response = new SayHiUserResponse();

        response.setResponse("Hi " + request.getFirstName() + " " + request.getLastName());

        log.info("End sayHiUserResponse " + response);
        return response;
    }   
}

spring xml文件(applicationContext.xml):

<bean id="listenerContainer"
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="mqCachedConnectionFactory" />
        <property name="destination" ref="defaultDestination" />
        <property name="messageListener">
            <bean class="org.springframework.ws.transport.jms.WebServiceMessageListener">
                <property name="messageFactory" ref="messageFactory"/>
                <property name="messageReceiver" ref="messageReceiver" />
            </bean>
        </property>
        <property name="concurrentConsumers" value="1" />
        <property name="acceptMessagesWhileStopping" value="false" />
        <property name="recoveryInterval" value="10000" />
        <property name="cacheLevelName" value="CACHE_CONSUMER" />
    </bean>

    <bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory" />    

    <bean id="messageReceiver" class="org.springframework.ws.soap.server.SoapMessageDispatcher">
        <property name="endpointAdapters">
            <list>
                <bean id="endpointAdapter" class="org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter">
                    <constructor-arg ref="marshaller" />
                </bean>
            </list>
        </property>         
    </bean>

    <bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping" />

为简单起见,我删除了不必要的代码和xml配置。

无论如何要保持我的端点类(MyEndpoint.java)并使用spring-integration-ws支持(ws:inbound-gateway和channel)?

如果您需要更多信息,请与我们联系。

由于

1 个答案:

答案 0 :(得分:0)

  

无论如何要保持我的端点类(MyEndpoint.java)并使用spring-integration-ws支持(ws:inbound-gateway和channel)?

是的,你可以。

我将尝试为您的情况解释一个简单的集成-ws实现:

<强>的applicationContext.xml:

<bean id="endpointMappings"
        class="org.springframework.beans.factory.config.ListFactoryBean"
        lazy-init="true">
        <property name="sourceList">
            <list>
                <ref bean="uriEndpointMapping"/>
            </list>
        </property>
</bean>

<bean id="uriEndpointMapping"
        class="org.springframework.ws.server.endpoint.mapping.UriEndpointMapping"
        p:defaultEndpoint-ref="ws-inbound-gateway">
        <property name="mappings">
            <props>
                <prop key="${yourEndPointUrl}">ws-inbound-gateway</prop>
            </props>
        </property>
</bean>

<int-ws:inbound-gateway id="ws-inbound-gateway"
                        request-channel="ws-request-channel"
                        reply-channel="ws-response-channel"
                        error-channel="ws-error-channel"
                        marshaller="marshaller"
                        unmarshaller="marshaller"/>

<int:channel id="ws-request-channel"/>
<int:channel id="ws-response-channel"/>
<int:channel id="ws-error-channel"/>

<int:chain input-channel="ws-request-channel" output-channel="ws-response-channel">
    <int:service-activator ref="yourBean" method="getUserResponse"/>
</int:chain>

这里有一些观点。 你需要定义一个bean,MyEndpoint类型,我在这里将它命名为 yourBean yourEndPointUrl 定义 getUserResponse 方法的终点网址。
我已在链中定义了服务激活器,您可以在此链中添加日志记录或其他内容以丰富您的处理机制 您还需要一个路由器,用于将请求路由到bean的不同方法。