gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:34:49 25 4
gpt4 key购买 nike

我们正在尝试将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com