【发布时间】:2015-06-03 12:54:34
【问题描述】:
我可能用这个来找错树了,但是我在使用 Spring Integration 和 http outbound-gateway 时遇到了一些困难。
我可以对其进行配置,使其生成一个 http POST,然后我得到一个简单的 String 响应正文,如下所示:
弹簧配置
<int-http:outbound-gateway request-channel="hotelsServiceGateway.requestChannel"
reply-channel="hotelsServiceGateway.responseChannel"
url="http://api.ean.com/ean-services/rs/hotel/v3/list"
expected-response-type="java.lang.String"
http-method="POST"/>
界面
public interface ExpediaHotelsService {
String getHotelsList(final Map<String, String> parameters);
}
我可以配置它,以便我得到一个ResponseEntity 这样的回复:
弹簧配置
<int-http:outbound-gateway request-channel="hotelsServiceGateway.requestChannel"
reply-channel="hotelsServiceGateway.responseChannel"
url="http://api.ean.com/ean-services/rs/hotel/v3/list"
http-method="POST"/>
界面
public interface ExpediaHotelsService {
ResponseEntity<String> getHotelsList(final Map<String, String> parameters);
}
这两个版本的代码都可以工作。但是,当返回 String 时,我得到了响应正文,但没有得到 http 状态和标头等。
但是当我使用ResponseEntity 版本时,我得到了http 状态和标题,但我总是通过ResponseEntity#getBody 得到一个空正文
我是否可以同时获取正文和 http 状态和标头?
(忽略 expedia 酒店 api 返回 JSON 的事实 - 目前我只想获取原始正文)
一些进一步的信息有助于澄清我看到的问题。如果我在响应通道上进行窃听:
当我将其配置为返回一个简单的String 时,我得到:INFO: GenericMessage [payload={"HotelListResponse":{"EanWsError":{"itineraryId":-1,"handling":"RECOVERABLE","category":"AUTHENTICATION","exceptionConditionId":-1,"presentationMessage":"TravelNow.com cannot service this request.","verboseMessage":"Authentication failure. (cid=0; ipAddress=194.73.101.79)"},"customerSessionId":"2c9d7b43-3447-4b5e-ad87-54ce7a810041"}}, headers={replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@4d0f2471, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@4d0f2471, Server=EAN, Connection=keep-alive, id=5e3cb978-9730-856e-1583-4a0847b8dc73, Content-Length=337, contentType=application/json, http_statusCode=200, Date=1433403827000, Content-Type=application/x-www-form-urlencoded, timestamp=1433403827133}]
您可以在有效负载中看到完整的响应正文,并注意到 Content-Length 被设置为 337
相反,当我使用 ResponseEntity<String> 时,我得到:INFO: GenericMessage [payload=<200 OK,{Transaction-Id=[5f3894df-0a8e-11e5-a43a-ee6fbd565000], Content-Type=[application/json], Server=[EAN], Date=[Thu, 04 Jun 2015 07:50:30 GMT], Content-Length=[337], Connection=[keep-alive]}>, headers={replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@4d0f2471, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@4d0f2471, Server=EAN, Connection=keep-alive, id=9a598432-99c9-6a15-3451-bf9b1515491b, Content-Length=337, contentType=application/json, http_statusCode=200, Date=1433404230000, Content-Type=application/x-www-form-urlencoded, timestamp=1433404230465}]Content-Length 仍然设置为 337,但负载中没有响应正文
【问题讨论】:
-
我也有同样的情况。你解决了吗? @内森·拉塞尔
-
我也有类似的要求。我通过定义预期的响应类型解决了这个问题,然后我添加了转换器,将 GenericMessage 转换为我的对象。我从 GenericMessage 标头中提取了 httpStatus,并从有效负载响应正文中提取了。
标签: java spring http spring-integration