[英]Spring http outbound gateway - how to return a ResponseEntity with a body
我可能为此惹恼了错误的树,但是我在使用Spring Integration和http出站网关时遇到了一些困难。
我可以对其进行配置,以使其进行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,但是有效负载中没有响应主体
请注意,对于第二种情况,您不使用任何expected-response-type
。
RestTemplate
在没有expected-response-type
情况下以这种方式工作:
public ResponseEntityResponseExtractor(Type responseType) {
if (responseType != null && !Void.class.equals(responseType)) {
this.delegate = new HttpMessageConverterExtractor<T>(responseType, getMessageConverters(), logger);
}
else {
this.delegate = null;
}
}
@Override
public ResponseEntity<T> extractData(ClientHttpResponse response) throws IOException {
if (this.delegate != null) {
T body = this.delegate.extractData(response);
return new ResponseEntity<T>(body, response.getHeaders(), response.getStatusCode());
}
else {
return new ResponseEntity<T>(response.getHeaders(), response.getStatusCode());
}
}
如您所见,它实际上返回没有body
的ResponseEntity
。 Spring Integration对此无能为力...
从另一面看,是否真的需要整个ResponseEntity
作为<int-http:outbound-gateway>
的回复。 也许headerMapper
?应该够你..例如http status
在这里已经存在,甚至在你的日志从这样的问题:
服务器= EAN,连接=保持活动状态,id = 5e3cb978-9730-856e-1583-4a0847b8dc73,Content-Length = 337,contentType = application / json,http_statusCode = 200,Date = 1433403827000,Content-Type = application / x- WWW窗体-urlencoded,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.