Spring xml Web 服务的集成 http-outbound-gateway 未返回响应数据
Spring Integration http-outbound-gateway for xml web service not returning response data
我们正在尝试将 http-outbound-request 连接到 Web 服务,其中 url 需要一个简单的 POST.
当我们尝试为此 Web 服务使用 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 网络服务 并获得成功响应。然后我们 return 将响应 xml 返回到主要的 http-outbound 元素
然而我们运气不好,我们从 "data-gateway" 得到的新回复仍然是
{
"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。
以下是完整的整合springxml.
<!-- 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
对象。
我们正在尝试将 http-outbound-request 连接到 Web 服务,其中 url 需要一个简单的 POST.
当我们尝试为此 Web 服务使用 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 网络服务 并获得成功响应。然后我们 return 将响应 xml 返回到主要的 http-outbound 元素
然而我们运气不好,我们从 "data-gateway" 得到的新回复仍然是
{
"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。
以下是完整的整合springxml.
<!-- 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
对象。