Nathan Russell
Nathan Russell

Reputation: 3658

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.

I can configure it so that it makes a http POST and I get the response body as a simple String like this:

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:

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.
But when I use the ResponseEntity version I get the http status and headers, but I always get a null body via ResponseEntity#getBody

Is there anyway I can get both the body and the http status and headers?
(Ignoring the fact that the expedia hotels api returns JSON - at the moment I just want to get the raw body text)


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:
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

Conversely, when I use a ResponseEntity<String> I get:
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

Upvotes: 1

Views: 2369

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121177

Notice that you don't use any expected-response-type for the second case.

The RestTemplate works this way in case of no 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. And Spring Integration can do nothing here on the matter...

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>. Maybe headerMapper would be enough for you?.. For example http status is here already, even in your logs from the question:

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,

Upvotes: 1

Related Questions