[英]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.