使用Spring集成发送Web服务请求

时间:2019-01-19 16:11:46

标签: spring-integration-http

我想使用spring集成来代替旧的Webservice网关。在旧网关中,程序接收请求并由httpclient发送出去。 这是我旧的Web服务网关的发送部分

 DefaultHttpClient httpClient = null;
    Map<String,String> map = new HashMap<String,String>();
    httpClient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(dynamicUrl);
    String soapRequestData =dynamicXMLString;
    HttpEntity re = new StringEntity(soapRequestData, HTTP.UTF_8);
    httppost.setHeader("Content-Type","application/soap+xml; charset=utf-8");
    httppost.setEntity(re);
    try {
        HttpResponse response = httpClient.execute(httppost);
        if(response.getStatusLine().getStatusCode() == 200) {  
            String xmlString = EntityUtils.toString(response.getEntity());
            System.out.println("-------------------------");
            System.out.println(xmlString);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

现在,我想使用spring-integration-http替换httpclient部分。 我使用int-http:inbound-gateway接收请求,使用服务激活器处理请求并通过int-http:outbound-gatewy发送出去。 入站网关:

    <int:channel id="inchannel"/>
<int:channel id="respReply"/>
<int-http:inbound-gateway request-channel="inchannel" path="**" supported-methods="POST,GET"
                          reply-channel="respReply">
    <int-http:cross-origin/>
</int-http:inbound-gateway>
<int:service-activator input-channel="receiveChannel" ref="serviceActivator" method="serviceActivator"
                       output-channel="respReply"></int:service-activator>

我的spirng集成出站网关是这样的:

    <int:channel id='reply.channel'>
    <int:queue capacity='10'/>
</int:channel>
<int:channel id='request.channel'/>


<int-http:outbound-gateway id="outbound.gateway"
                           request-channel="request.channel" url-expression="headers.fwdUrl"
                           http-method-expression="headers.reqMethod"
                           charset="UTF-8" reply-timeout="5000" reply-channel="reply.channel"
>

我在激活器中的Java代码

 private GatewaySerive gatewaySerive;
public Message<?> serviceActivator(Message<String> msg){
    MessageChannel outRequestChannel = context.getBean("request.channel", MessageChannel.class);
    PollableChannel outReplyChannel = context.getBean("reply.channel", PollableChannel.class);
    gatewaySerive.getSendOutMsg(msg); // add headers.fwdUrl headers.reqMethod in msg head
    try{
        outRequestChannel.send(enhanceMsg);
    }catch (Exception e){
        e.printStackTrace();
        System.out.println(e.getMessage());
    }
    Message<?> replyMesg = outReplyChannel.receive();

并且在发送Web服务请求时出现异常:

org.springframework.messaging.MessageHandlingException: HTTP request execution failed for URI [http://xxx.asmx]; nested exception is org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error, failedMessage=GenericMessage [payload=60,63,120,109,108,32,118,101,114,115,105,111,xxxxxxxxxxx, headers={content-length=317, http_requestMethod=POST, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@348c0b2c, httpMethod=POST, replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@348c0b2c, respUrl=http://xxx.asmx, host=localhost:8080, http_requestUrl=http://localhost:8080/httpGateway/ws/findPhonePlace, connection=Keep-Alive, id=7450b3a9-7432-8740-f5fa-0914dcbb1b81, contentType=application/soap+xml;charset=utf-8, accept-encoding=gzip,deflate, user-agent=Apache-HttpClient/4.5.6 (Java/1.8.0_131), timestamp=1547907719856}]

我该如何解决呢?我想通过spring-integration-http

发送网络服务

0 个答案:

没有答案