java - Spring Integration http-outbound-gateway 用于 xml web 服务不返回响应数据

标签 java spring web-services spring-integration

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

当我们尝试使用 http-outbound-gateway 时,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"
}

然后,我们尝试在转换器元素中使用以下示例代码,并通过不同的 http-inbound 元素调用它。之后,我们将上面的 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 对象。

关于java - Spring Integration http-outbound-gateway 用于 xml web 服务不返回响应数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28249819/

相关文章:

java - Firestore 在 recyclerview 中检索具有限制的文档

java - 无法在 SQL Server 中插入查询

java - 传入 JMS 消息的事务同步未激活

java - 如何将 JAXB 转换为 JSON?

java - 使用 Mockito 为扩展泛型类的类创建模拟

java - Selenium WD/Java |无法创建 'if' 和 'else if' 语句

mysql - 数据未使用 hibernateTemplate 类插入数据库

java - Spring 分段上传

java - 在 JAXB 中将 JSON 对象转换为字符串

spring - 无法找到 XML 模式命名空间的 Spring NamespaceHandler [http ://cxf. apache.org/jaxws] - 包括 cxf-rt-frontend-jaxrs