Spring Integration DSL 变压器

Posted

技术标签:

【中文标题】Spring Integration DSL 变压器【英文标题】:Spring Integration DSL transformer 【发布时间】:2017-12-22 15:14:43 【问题描述】:

我可以在以下问题上获得帮助吗? 调用转换器将输入对象转换为映射对象并调用处理程序,处理程序缺少之前添加的标头值。 为什么在将有效负载转换为 Map 对象时会丢失所有标头?

//Adding header here setHeader("t", "t");
@ResponseBody
public EmResponse getAuditTrail(@Valid @RequestBody NGAuditTrailEntry auditEntry) 
    LOG.info("Audit Service Called, creating new audit " + auditEntry);
    AuditCreationFlow.CreateAuditGateway auditGateway = applicationContext.getBean(AuditCreationFlow.CreateAuditGateway.class);
    MessageBuilder messageBuilder = MessageBuilder.withPayload(auditEntry).setHeader("t", "t");
    Object response = auditGateway.createAudit(messageBuilder.build());
    EmResponse res = new EmResponse();
    LOG.info("Done with Audit creation. Response " + response);
    return res;

//Integration flow starts here
public IntegrationFlow createAuditGatewayFlow() 
    LOG.debug("Entered to spring integration flow to create the Audit entry");
    return IntegrationFlows.from("auditInputChannel")
        .handle(auditObjTransformer, "transformToEjbCompatible")
        .handle(ejbCaller, "callEjb")
        .get();

//Transforming payload object to map
@Component
public class AuditObjTransformer 
    private final Logger LOG = LoggerFactory.getLogger(this.getClass());

    @Transformer
    public Object transformToEjbCompatible(NGAuditTrailEntry ngAuditTrailEntry, Map<String, Object> headers) 
        LOG.debug("Transforming the NGAuditTrailEntry To AuditEntry object which is EJB compatible");

        //@TODO - Tranformation code goes here.

        String s = ngAuditTrailEntry.getObjectName();
        Map<String, String> m = new HashMap<>();
        m.put("x", s);
        return m;
    
//Here in this handler, not getting headers what I added in the rest service above.
public class EJBCaller 
    private final Logger LOG = LoggerFactory.getLogger(this.getClass());

    public Object callEjb(Object payload, Map<String, Object> headers) throws EJBResponseException 
        LOG.debug("Calling Audit EJB to crated Audit entry.");

        //@TODO EJB calling code goese here.

        LOG.debug("Returned from EJB after creating Audit entry. Returned value" + payload);
        return payload;
    

如果转换不是地图,那么标题中没有问题。

谢谢, 湿婆

【问题讨论】:

【参考方案1】:
 callEjb(Object payload, Map<String, Object> headers) 

如果有效负载是Map,那么您在payloadheaders 方法参数中同时拥有该有效负载。

要使其正常工作并将 headers 准确地传递到 Map 参数,您应该在其上使用 @Headers 注释:

 * Annotation which indicates that a method parameter should be bound to the headers of a
 * message. The annotated parameter must be assignable to @link java.util.Map with
 * String keys and Object values.

【讨论】:

以上是关于Spring Integration DSL 变压器的主要内容,如果未能解决你的问题,请参考以下文章

在 Spring Integration DSL 中使用带有丢弃通道的过滤器

Spring Integration DSL:PublishSubscribeChannel 订单

Spring Integration DSL JMS 入站/出站网关

Spring Integration DSL 中的路由

如何将请求标头添加到 outboundGateway spring integration dsl

使用 Spring Integration DSL 读取 Tibco EMS 主题