2016-07-22 37 views
3

我打電話使用Spring集成<int-http:outbound-gateway>一個春數據休息 URL響應空有效載荷。Spring集成與Spring數據休息:從出站網關

我的調用是一個簡單的HTTP GET朝着一個已知的資源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"配置,但我不知道這個警告是否與問題相關。

這是與消息的出站信道的包含REST URL要調用的調用:

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頭看。

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

如果我直接去與在瀏覽器上得到那個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結果嗎?

在此先感謝。

UPDATE

正如加里的建議,我已監控的流量。

這裏有請求的詳細信息:

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 

的要求似乎是一樣的,除了一個頭(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 
    } 
} 

發現爲什麼將this.delegate設置爲null會很有趣。

+0

請求中可能有不同的內容。我建議你使用TCP監視器(有一個內置於eclipse中,或使用wireshark等)來比較兩個請求/響應。如果你無法從中找出答案,請用網絡跟蹤編輯問題。 –

+0

謝謝你的建議,加里。我已經編輯了關於請求細節的問題。 –

+0

聽起來像你的服務器應用程序可能需要一個會話(登錄?) - 你需要在服務器端進行調試。 –

回答

3

您需要將expected-response-type="java.lang.String"添加到網關。

+0

它工作!謝謝! –