Spring Integration with Spring Data Rest:来自出站网关的空负载响应



我正在使用带有<int-http:outbound-gateway>Spring Integration调用Spring Data RestURL。

我的调用是一个简单的HTTPGET,指向一个已知的资源URL(在我的例子中是"examples")。

这是<int-http:outbound-gateway>:的配置

<int-http:outbound-gateway id="internalGW" request-channel="dataRestChannel"
    encode-uri="true" url-expression="payload" 
    http-method="GET" extract-request-payload="true"
    header-mapper="headerMapper"> 
</int-http:outbound-gateway>

在日志中,我可以看到以下消息:

"extractPayload"属性与当前请求,因为HTTP方法是"GET",并且不会有请求正文发送该方法。

我想这是指http-method="GET" extract-request-payload="true"配置,但我不知道这个警告是否与这个问题有关。

这是对出站通道的调用,其中包含要调用的RESTurl的消息:

public Object invokeUrl(String url){
    MessagingChannel messagingChannel = (MessagingChannel)ApplicationContextResolver.getApplicationContext().getBean("requestChannelBean");
    MessageChannel dataRestChannel = messagingChannel.getDataRestChannel();
    MessagingTemplate messagingTemplate = new MessagingTemplate();
    Message<?> requestMessage = MessageBuilder.withPayload(url).build();
    Message<?> response = messagingTemplate.sendAndReceive(dataRestChannel, requestMessage);
    return response.getPayload();
}

调用很好,我有一个HTTP状态代码200;这是响应.getPayload():

<200 OK,
{
    Server=[Apache-Coyote/1.1],
    Cache-Control=[no-cache,no-store,max-age=0,must-revalidate],
    Pragma=[no-cache],
    Expires=[0],
    X-XSS-Protection=[1; mode=block],
    X-Frame-Options=[DENY,DENY],
    X-Content-Type-Options=[nosniff,nosniff],
    Transfer-Encoding=[chunked],
    Date=[Fri,22 Jul 2016 13:03:22 GMT],
    Content-Type=[application/hal+json],
    Content-Length=[0]
}>

然而,我不明白为什么"有效负载主体"是空的,正如您在Content-Length标头中看到的那样。

response    GenericMessage<T>  (id=219) 
    headers MessageHeaders  (id=221)    
    payload ResponseEntity<T>  (id=222) 
        body    null    
        headers HttpHeaders  (id=224)   
        statusCode  HttpStatus  (id=159)    

如果我在浏览器上用GET直接访问该URL,这就是我收到的响应:

{
    "_embedded": {
        "examples": []
    },
    "_links": {
        "self": {
            "href": "http://localhost:8080/restapp/api/examples"
        },
        "profile": {
            "href": "http://localhost:8080/restapp/api/profile/examples"
        }
    }
}

我希望这是响应有效负载,而不是空的有效负载

我收到的ResponseEntity.body似乎是空的。

有没有任何方法可以通过出站网关获得HTTP get的相同JSON结果?

提前谢谢。

更新

根据加里的建议,我已经监控了交通。

以下是请求的详细信息:

春季集成请求:

GET http://localhost:8080/restapp/api/examples HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch
Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4

浏览器请求:

GET http://localhost:8080/restapp/api/examples HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch
Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: JSESSIONID=C966FB7DB6B172D530D7C1F4DC57C6B8

除了一个标头(Cookie JSESSIONID,Integration上下文中没有)之外,请求似乎是一样的。

更新2

也许我已经在响应中找到了空负载的原因。

org.springframework.web.client.RestTemplate"extractData"方法将对象实例this.delegate设置为空值

因此,作为回报,存在一个没有正文的ResponseEntity对象。

@Override
public ResponseEntity<T> extractData(ClientHttpResponse response) throws IOException {
    if (this.delegate != null) {  // this.delegate is 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()); // <- it's executed this one
    }
}

如果能发现这个.delegate被设置为null的原因,那将是一件有趣的事情。

您需要将expected-response-type="java.lang.String"添加到网关。

最新更新