首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring集成http-出站-不返回响应数据的xml web服务网关

Spring集成http-出站-不返回响应数据的xml web服务网关
EN

Stack Overflow用户
提问于 2015-01-31 08:48:21
回答 1查看 1.8K关注 0票数 1

我们正在尝试将一个http-出站请求连接到一个web服务,其中url需要一个简单的帖子。

当我们尝试使用http-出站网关时,web服务如下所示

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

代码语言:javascript
复制
{
    "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"
}

然后,我们尝试在转换器元素中使用以下示例代码,并让调用它--一个不同的http-入站元素。之后,我们将上面的http-出站元素超链接到http-入站元素,该元素调用转换器,如下所示

代码语言:javascript
复制
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;
}

上面的代码将调用实际的web服务并获得成功的响应。然后,我们将响应xml返回到主http-出站元素。

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

代码语言:javascript
复制
{
    "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。

代码语言:javascript
复制
    <!-- 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" />
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-31 13:59:10

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

代码语言:javascript
复制
expected-response-type="java.lang.String"

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28249819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档