2016-02-05 191 views
1

我想調用POST方法,它需要標題和正文。如何評估網關中的「Header」和「Payload」?

RequestGateway.java

import org.springframework.messaging.handler.annotation.Header; 
import org.springframework.messaging.handler.annotation.Payload;  
import java.util.Map; 

public interface RequestGateway { 

// Using this throws error 
// This complains saying no more than one Payload parameter 
// String process(Map<String, String> request, String x, String s); 

    String process(@Payload Map<String, String> request, @Header("accountReferenceId") String x, @Header("profileReferenceId") String s); 

} 

的Java類

public void test(String accountReferenceId, String profileReferenceId) { 
..... 
..... 
     RequestGateway paymentRequestGateway = context.getBean("paymentRequestGateway", RequestGateway.class); 
     String paymentResponse = paymentRequestGateway.process(paymentRequestMap, accountReferenceId, profileReferenceId); 
} 

SIConfig.xml

<!--HEADER MAPPER--> 
<bean id="headerMapper" 
     class="org.springframework.integration.http.support.DefaultHttpHeaderMapper"> 
    <property name="inboundHeaderNames" value="*" /> 
    <property name="outboundHeaderNames" value="HTTP_REQUEST_HEADERS, Api-Key, Accept, Content-Type" /> 
    <property name="userDefinedHeaderPrefix" value="" /> 
</bean> 

<int:gateway id="paymentRequestGateway" 
      service-interface="com.abc.api.payments.inboundpayments.api.RequestGateway" 
      default-request-channel="PaymentRequestChannel" 
      default-reply-channel="ResponseChannel"> 
    <int:default-header name="Accept" value="application/json; v=5"/> 
    <int:default-header name="Content-Type" value="application/json; v=5"/> 
    <int:default-header name="accountReferenceId" expression="#args[1]"/> 
    <int:default-header name="profileReferenceId" value="test"/> 
</int:gateway> 

<int-http:outbound-gateway 
     id="Payment Outbound Gateway" 
     request-channel="PaymentRequestChannel" 
     reply-channel="ResponseChannel" 
     error-handler="errorHandler" 
     request-factory="sslFactory" 
     header-mapper="headerMapper" 
     url="https://localhost:9090/accounts/{accountReferenceId}/payments?profileReferenceId={profileReferenceId}" 
     http-method="POST" 
     expected-response-type="java.lang.String" 
     extract-request-payload="true"> 
    <int-http:uri-variable name="accountReferenceId" expression="headers['accountReferenceId']" /> 
    <int-http:uri-variable name="profileReferenceId" expression="headers['profileReferenceId']" /> 
</int-http:outbound-gateway> 

在默認header expression =「#args [1]」沒有得到正確評估。當我直接把價值,如<int:default-header name="accountReferenceId" value="test"/>,它工作正常。

我在做什麼這裏的表達錯誤?

+1

它是如何評估呢?我剛剛在本地進行了測試,運行良好。即使使用類似的@Header(「accountReferenceId」)/ 'mix。請解釋你的目標:應該如何。你期望看到什麼? –

+0

嗨Artem,如果我調用'test(abc,def);'默認頭文件中的url-expression應該被評估爲'https:// localhost:9090/accounts/abc/payments?profileReferenceId = def',doesn'當我給予直接價值時,它似乎正在發生這樣的事情。我不知道這是如何評估當前,也許我應該設置日誌級別進行調試? –

+1

那麼,這看起來像一些混合。你不用'* two args *'顯示方法* ...對於'org.springframework.integration'配置DEBUG來查看'headers'的外觀是很好的。從另一方面來說,直接從你身邊調試SI代碼會更好。或者更好地提出一些簡單的測試案例來重現我們的方面。 –

回答

0

爲框架類別打開DEBUG總是好的,看看執行過程中發生了什麼。

在我們的情況下,它是org.springframework.integration與調試,你可以在日誌中像看到:

QueueChannel: (main) preSend on channel 'channel', message: GenericMessage [payload=org.springframework.context.event.ContextRefreshedEvent[source=org[email protected]17f6480: startup date [Fri Feb 05 18:36:32 EST 2016]; root of context hierarchy], headers={id=8e832449-cc71-bd19-45d2-a396d553cdd5, timestamp=1454715393215}] 

隨着在手,你可以分析你自己的頭,確定信道,並試圖找出誰是有罪的。

另外請記住,Spring集成提供Message History模式實施,其中與日誌,您可以trace消息旅程。