简体   繁体   English

Spring HTTP出站网关-如何返回带有主体的ResponseEntity

[英]Spring http outbound gateway - how to return a ResponseEntity with a body

I'm probably barking up the wrong tree with this, but I'm having some difficulty with Spring Integration and a http outbound-gateway. 我可能为此惹恼了错误的树,但是我在使用Spring Integration和http出站网关时遇到了一些困难。

I can configure it so that it makes a http POST and I get the response body as a simple String like this: 我可以对其进行配置,以使其进行http POST ,然后将响应正文作为一个简单的String如下所示:

Spring Config 弹簧配置

<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"/>

Interface 接口

public interface ExpediaHotelsService {
    String getHotelsList(final Map<String, String> parameters);
}

And I can configure it so that I get a ResponseEntity back like this: 而且我可以对其进行配置,以便像下面这样获得ResponseEntity

Spring Config 弹簧配置

<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"/>

Interface 接口

public interface ExpediaHotelsService {
    ResponseEntity<String> getHotelsList(final Map<String, String> parameters);
}

Both versions of the code work. 两种版本的代码都可以工作。 However, when returning a String I get the response body, but I don't get the http status and headers etc. 但是,当返回一个String我得到了响应主体,但没有得到http状态和标头等。
But when I use the ResponseEntity version I get the http status and headers, but I always get a null body via ResponseEntity#getBody 但是,当我使用ResponseEntity版本时,我会获得http状态和标头,但是我总是通过ResponseEntity#getBody获得一个空主体

Is there anyway I can get both the body and the http status and headers? 无论如何,我可以同时获取正文和http状态以及标头吗?
(Ignoring the fact that the expedia hotels api returns JSON - at the moment I just want to get the raw body text) (忽略了Expedia酒店api返回JSON的事实-此刻,我只想获取原始正文)


Some further info which helps clarify the problem I am seeing. 一些进一步的信息有助于澄清我所遇到的问题。 If I put a wire-tap on the response channel: 如果我在响应频道上进行窃听:

When I've configured it to return a simple String I get: 当我配置它返回一个简单的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}]
You can see the full response body in the payload, and notice the Content-Length being set to 337 您可以在有效负载中看到完整的响应主体,并注意到Content-Length设置为337

Conversely, when I use a ResponseEntity<String> I get: 相反,当我使用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}]
The Content-Length is still set to 337, but there is no response body in the payload Content-Length仍设置为337,但是有效负载中没有响应主体

Notice that you don't use any expected-response-type for the second case. 请注意,对于第二种情况,您不使用任何expected-response-type

The RestTemplate works this way in case of no 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());
        }
    }

As you see it really returns the ResponseEntity without body . 如您所见,它实际上返回没有bodyResponseEntity And Spring Integration can do nothing here on the matter... Spring Integration对此无能为力...

From other side let's take a look if you really need a whole ResponseEntity as a reply back from the <int-http:outbound-gateway> . 从另一面看,是否真的需要整个ResponseEntity作为<int-http:outbound-gateway>的回复。 Maybe headerMapper would be enough for you?.. For example http status is here already, even in your logs from the question: 也许headerMapper ?应该够你..例如http status在这里已经存在,甚至在你的日志从这样的问题:

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, 服务器= 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM