Spring 与 Spring Data Rest : empty payload in response from outbound-gateway 的集成

标签 spring spring-integration spring-data-rest

我调用 Spring Data Rest URL 使用 Spring集成<int-http:outbound-gateway> .

我的调用很简单 HTTP GET 指向一个已知的资源 URL(在我的例子中,“示例”)。

这是<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>

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

The 'extractPayload' attribute has no relevance for the current request since the HTTP Method is 'GET', and no request body will be sent for that method.



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

这是使用包含要调用的 REST url 的消息调用该出站 channel :
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;这是 response.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 header 中看到的那样。
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 结果?

提前致谢。

更新

根据加里的建议,我已经监控了流量。

以下是请求的详细信息:

Spring集成请求:
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

除了一个 header (Cookie JSESSIONID,集成上下文中没有),请求似乎是相同的。

更新 2

也许我已经找到了响应中负载为空的原因。
"extractData" org.springframework.web.client.RestTemplate的方法具有对象实例 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
    }
}

发现为什么 this.delegate 被设置为 null 会很有趣。

最佳答案

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

关于Spring 与 Spring Data Rest : empty payload in response from outbound-gateway 的集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38527896/

相关文章:

Spring Data JPA - 为什么对返回的实体的更改会自动持久化?

java - Spring Integration xml 到 java dsl - 如何定义入站/出站 channel 适配器、轮询器等

java - 在 RabbitMQ 中组接收消息,最好使用 Spring AMQP?

java - 将自定义端点添加到 Spring Data REST 存储库并在 Swagger 中显示

java - 通过 Spring Security 和 SpEL 表达式限制 POST 和 REST 请求时出错

java - 如何在 Spring 中捕获 JSP 错误?

java - 如何在 Spring 为 map 中未找到的键设置默认值

java - 从模板生成 Spring 上下文文件

spring-mvc - 将 Spring MVC 与 Spring Integration 集成

java - 为什么我的@RestController 中接收到的实体将值设置为 null 而不是默认值