Spring Integration Webservices

时间:2016-08-29 15:02:01

标签: spring-integration

我在spring集成时非常新,我正在尝试将以下代码转换为spring集成代码,但我无法正确发送请求。 我真的不知道自己做错了什么,做什么,实际上任务是拆分发送到webservices的XML,然后从中获取响应,然后聚合使其成为一个XML。 普通代码:

connection = (HttpURLConnection) validateServer.openConnection();
                connection.setRequestMethod("POST");
                connection.setDoInput(true);
                connection.setDoOutput(true);
                String parameters = "";
                if (isMultipleVal) {
                    parameters = ADD_FUNCTION2
                            + URLEncoder.encode(getxmldatalist, "UTF-8");
                } else {
                    parameters = ADD_FUNCTION
                            + URLEncoder.encode(getxmldatalist, "UTF-8");
                }
                LOG.info("parameters:" + parameters);
                System.out.println("In client parameters "+parameters);
                byte[] parameterAsBytes = parameters.getBytes();

                System.out.println("Parameters as bytes "+parameterAsBytes);
                connection.setRequestProperty(CONTENT_LENGTH,String.valueOf(parameterAsBytes.length));

                OutputStream oStream = connection.getOutputStream();
                oStream.write(parameterAsBytes, 0, parameterAsBytes.length);
                oStream.flush();
                oStream.close();

Spring Integration代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:int-xml="http://www.springframework.org/schema/integration/xml" 
    xmlns:int-ws="http://www.springframework.org/schema/integration/ws" 
    xmlns:int-http="http://www.springframework.org/schema/integration/http"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int-stream="http://www.springframework.org/schema/integration/stream"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
    http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd 
    http://www.springframework.org/schema/integration/xml http://www.springframework.org/schema/integration/xml/spring-integration-xml.xsd
    http://www.springframework.org/schema/integration/ws http://www.springframework.org/schema/integration/ws/spring-integration-ws.xsd
    http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">

<int:channel id="requestGatewayChannel"/>

<int:gateway id="serviceGateway" service-interface="xxx.xxx.xxx"    default-request-channel="requestGatewayChannel" default-reply-channel="replyGatewayChannel" />

<int:channel id="replyGatewayChannel"/>

<int:splitter ref="rootSplitter" input-channel="requestGatewayChannel" output-channel="splitterOutputChannel" method="splitRoot"/>

<int:channel id="splitterOutputChannel"/>

<bean id="rootSplitter" class="xxx.xxx.xxx.VehicleRootSpliter" />

<int-xml:marshalling-transformer id="marshaller" input-channel="splitterOutputChannel" output-channel="marshallerOutputChannel" marshaller="marshallerUnmarshaller" result-type="StringResult" />

<int:channel id="marshallerOutputChannel"></int:channel>

<int:object-to-string-transformer input-channel="marshallerOutputChannel" output-channel="stringOutputChannel"></int:object-to-string-transformer>

<int:channel id="stringOutputChannel"></int:channel>

<int:transformer id="dataTransformer" input-channel="stringOutputChannel" output-channel="dataTransformerOutputChannel" ref="xmlToEncode" method="dataTransformer"></int:transformer>
---To encode the string of xml using custom transformer---
<int:channel id="dataTransformerOutputChannel"></int:channel>

<bean id="xmlToEncode" class="xxx.xxx.xxx.DataTransformer"></bean>

<int:header-enricher default-overwrite="true" input-channel="dataTransformerOutputChannel" output-channel="enricherOutputChannel">

    <int:header name="Content_length" ref="headerBean" method="valueLength"></int:header>

</int:header-enricher>

<int:channel id="enricherOutputChannel"></int:channel>

<bean id="headerBean" class="xxx.xxx.xxx.DataTransformer"></bean>

<int-http:outbound-gateway url="${toms.ws.url}" request-channel="enricherOutputChannel" reply-channel="httpOutputChannel" 
    header-mapper="headerMapper" http-method="POST" 
    expected-response-type="java.lang.String"/>
<bean id="headerMapper" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
    <property name="outboundHeaderNames" value="HTTP_REQUEST_HEADERS, Content_length"></property>
    <property name="userDefinedHeaderPrefix" value=""></property>
</bean>

<int:channel id="httpOutputchannel"></int:channel>

<int:service-activator input-channel="httpOutputChannel" ref="webserviceActivator" method="checkerWeb"></int:service-activator>

<bean id="webserviceActivator" class="xxx.xxx.xxx"></bean>

<bean id="marshallerUnmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller" >
    <property name="classesToBeBound">
        <list>
            <value>com.xxx.xxx.xxx.VRoot</value>
        </list>
    </property>
</bean>
</beans>

0 个答案:

没有答案