繁体   English   中英

用于XML Web Service的Spring Integration http-outbound-gateway不返回响应数据

[英]Spring Integration http-outbound-gateway for xml web service not returning response data

我们正在尝试将http-outbound-request连接 URL需要简单POST 的Web服务

当我们尝试为此使用http-outbound-gateway时,Web服务如下

可能会调用Web服务,因为没有错误,但是我们得到的响应如下

{
    "headers": {
        "Date": [
            "Sat, 31 Jan 2015 08:35:14 GMT"
        ],
        "Server": [
            "Apache-Coyote/1.1"
        ],
        "Content-Type": [
            "text/xml;charset=UTF-8"
        ],
        "Content-Length": [
            "234"
        ]
    },
    "body": null,
    "statusCode": "OK"
}

然后,我们尝试在转换器元素中使用以下示例代码,并由A DIFFERENT http-inbound element调用了它。 之后,我们将上述http-outbound元素超链接到http-inbound元素,该元素调用了转换器,如图所示

public String sendRequest(Message<?> data) {
    System.out.println(data);
    String allData = "";

    try {
        URL url = new URL("https://www.someurl.com/service");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setDoOutput(true);
        conn.setRequestMethod("POST");

        //Add headers from http outbound gateway
        for(Entry<String, Object> that : data.getHeaders().entrySet()){
            String key = that.getKey();
            Object value = that.getValue();

            if(Arrays.asList(HTTP_REQUEST_HEADER_NAMES).contains(key)){
                System.out.println("ADDING : " + key + " = " + value);
                conn.setRequestProperty(key, value.toString());
            }
        }

        OutputStream os = conn.getOutputStream();
        os.write(((String) data.getPayload()).getBytes());
        os.flush();

        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

        String output;
        while ((output = br.readLine()) != null) {
            allData += output;
        }

        conn.disconnect();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println(allData);
    return allData;
}

上面的代码段将调用ACTUAL Web服务并获得成功的响应。 然后,我们将响应xml返回到主要的http-outbound元素

但是,我们没有运气,但从“数据网关”获得的新回应仍然是

{
    "headers": {
        "Cache-Control": [
            "no-cache"
        ],
        "Content-Type": [
            "text/plain;charset=ISO-8859-1"
        ],
        "Content-Length": [
            "234"
        ],
        "Server": [
            "Jetty(8.1.14.v20131031)"
        ]
    },
    "body": null,
    "statusCode": "OK"
}

还要注意,服务器json属性值现在是我们自己的服务器jetty。

以下是完整的集成spring xml。

    <!-- MAIN FLOW -->
        <int:channel id="requestChannel"/>
        <int:channel id="responseChannel"/>

        <int-http:inbound-gateway supported-methods="POST"
            request-channel="requestChannel"
            reply-channel="responseChannel"
            path="/services/testData"
            reply-timeout="50000" />

        <int:transformer input-channel="requestChannel" output-channel="requestDataChannel" ref="requestGenerate" method="createRequest" />

        <int:channel id="requestDataChannel" />

        <int-http:outbound-gateway id="data-gateway"
                                   request-channel="requestDataChannel" 
                                   reply-channel="requestDataDisplayChannel"
                                   url="http://localhost:8080/rest-http/services/testRequest"
                                   <!-- we first tried directly calling the "https://www.someurl.com/service" directly from here which didn't work either -->
                                   http-method="POST"
                                   extract-request-payload="true"/>

        <int:channel id="requestDataDisplayChannel" />

        <int:transformer input-channel="requestDataDisplayChannel" output-channel="responseChannel" ref="requestGenerate" method="responseDisplay" />   




        <!-- TEST DUMMY WEB SERVICE WHICH ALSO CALLS THE ACTUAL WEB SERVICE SUCCESSFULLY THEN -->
        <int:channel id="requestSendChannel"/>
        <int:channel id="responseSendChannel"/>

        <int-http:inbound-gateway supported-methods="POST"
            request-channel="requestSendChannel"
            reply-channel="responseSendChannel"
            path="/services/testRequest"
            reply-timeout="50000" />

        <int:transformer input-channel="requestSendChannel" output-channel="responseSendChannel" ref="requestGenerate" method="sendRequest" />



        <!-- this is the class which contains all of the transformer java code including the java code shown above -->
        <bean name="requestGenerate" id="requestGenerate" class="org.application.RequestGenerate" />

您需要在出站网关上配置预期的响应类型。 例如:

expected-response-type="java.lang.String"

否则,结果是带有null主体的HttpResponse对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM