当前位置: 首页 > 知识库问答 >
问题:

Spring集成DSL变压器

袁文景
2023-03-14

在下面的问题上,我可以得到一些帮助吗:调用transformer将输入对象转换为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;
    }

如果转换不是map,则标头中没有问题。

谢了湿婆

共有1个答案

翟曦
2023-03-14
 callEjb(Object payload, Map<String, Object> 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的初学者,任何帮助都将非常感谢。

  • 上面的代码可以工作,客户端得到“处理请求中的错误”,服务器日志中有一个条目“Timeout exception is through”。但我在日志中也看到以下异常: MyTransformer的实现似乎不正确。 你能帮我定制变压器吗?如何在转换方法中获得有效负载,这样我就可以回复客户机作为‘处理请求的错误。有效载荷='+有效载荷? 非常感谢Artem的完美解决方案。

  • 我已经建立了一个简单的Spring集成流程,该流程由以下步骤组成: 然后定期轮询一个rest api 对有效载荷做一些处理 并将其置于Kafka主题上。 请遵守以下代码: 这非常有效,然而,我正在努力想出一些好的测试。 我应该如何模拟外部RESTAPI

  • 我有一个要求,我需要保存/缓冲在通道上接收到的消息,并根据消息数量或超时意味着1分钟内没有接收到消息而持久化在数据库中。有没有办法在Spring集成中实现这一点

  • 我已经使用最新的可用版本建立了一个新的Spring Boot Spring Integration Spring Integration Java DSL项目。项目构建正常,但当我运行应用程序时,我得到: 当前使用的依赖项如下: 错误可能是由于jar版本的错误组合吗?我不确定如何调试此错误。

  • 我想创建一个简单的IntegrationFlow与Spring集成,我有困难。 我想创建一个集成流,从Rabbit Mq中的多个队列中获取消息,并将消息发布到不同的Restendpoint。 我想知道我是否可以并行化这个。 我想检查两个场景的可行性: 首先,我想为每个RabbitMq队列创建一个线程,该队列在接收到消息后将侦听并执行流: 场景1 第二个场景:在这个场景中,我想为每个队列创建一个动态