Spring Integration Transformer 的表达式以将值设置为有效负载

Spring Integration Transformer's expression to set value to payload

我正在尝试使用 Spring 集成转换器的表达式来为有效负载设置一个值。

我的负载是一个具有以下变量的对象

Private int id;
Private String name;

//getter
//setter

这是我的变压器的样子

<int:transformer input-channel="inChannel"
    output-channel="outChannel"
    expression="payload.name = ‘test’”/>

但我无法让它工作。我在这里有什么错误吗?

提前致谢!

在大多数情况下,当您在 Spring 集成通道数据适配器中看到 SpEL 表达式时,它与“只读”行为有关。只是因为端点是双向逻辑:从输入通道接收消息,执行表达式,根据表达式评估结果构建回复并将其产生到输出通道。

“设置值”表达式没有 return 任何内容,因此即使我们可以支持 write 这种表达式,此转换器也不知道下一步该怎么做。

幸运的是,您正在寻找一个组件。 这是一个Content Enricher EI pattern implementation. In Spring Integration we call it <payload-enricher>. See docs for more info: https://docs.spring.io/spring-integration/reference/html/message-transformation.html#payload-enricher. See also this sample: https://github.com/spring-projects/spring-integration-samples/tree/main/basic/enricher