如何评估网关中的“标题”和“有效负载”?

时间:2016-02-05 02:12:29

标签: spring spring-integration

我正在尝试调用一个需要标题和正文的POST方法。

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>

在默认标题 expression =“#args [1]”未正确评估。当我直接输入值<int:default-header name="accountReferenceId" value="test"/>时,它可以正常工作。

这里的表达方式我做错了什么?

1 个答案:

答案 0 :(得分:0)

最好打开框架类别的DEBUG,看看执行的内容是什么。

在我们的例子中,它是org.springframework.integration,您可以在日志中看到类似的调试:

QueueChannel: (main) preSend on channel 'channel', message: GenericMessage [payload=org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.support.ClassPathXmlApplicationContext@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 Integration提供了Message History模式实现,通过日志,您可以trace消息之旅。