我们正在尝试将 http-outbound-request连接到网络服务,其中网址需要简单的POST。
当我们尝试将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"
}
然后我们尝试在变换器元素中使用以下示例代码,并通过一个不同的http入站元素进行调用。之后我们将上面的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元素
然而,我们没有运气,而且我们仍然得到了来自&#34;数据网关&#34;是
{
"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" />
答案 0 :(得分:2)
您需要在出站网关上配置预期的响应类型; e.g:
expected-response-type="java.lang.String"
否则,结果是HttpResponse
具有null
正文的对象。