【发布时间】:2020-01-22 17:24:31
【问题描述】:
我目前有一个 IntegrationFlow 实现,它利用 Service 类来实现要由流执行的所有所需功能。像这样的...
@Service
public class FlowService {
public Message<String> removeLineFeeds(Message<String> message) {
return MessageBuilder
.withPayload(StringUtils.remove(message.getPayload(), StringUtils.LF))
.copyHeadersIfAbsent(message.getHeaders())
.build();
}
}
@Configuration
@EnableIntegration
public class FlowConfiguration {
@Autowired
private FlowService flowService;
@Bean
public IntegrationFlow flow() {
return IntegrationFlows
.from("inputChannel")
.transform(flowService, "removeLineFeeds")
.get();
}
}
上述实现完全按照预期工作,但我希望改进/修改实现以利用 Java 8/Lambdas 的强大功能,使其看起来像这样......
@Bean
public IntegrationFlow flow() {
return IntegrationFlows
.from("inputChannel")
.transform(flowService::removeLineFeeds)
.get();
}
不幸的是,当以这种方式实现时,流程将在处理消息时抛出ClassCastException。我已经尝试了一些目前在线存在的不同提议的解决方案,但它们似乎都没有奏效。无论使用何种 IntegrationFlow 方法(转换、过滤器等),我都会遇到类似的问题。
需要对当前实现进行哪些更改以允许在 IntegrationFlow 方法中使用flowService::removeLineFeeds?
编辑:PER ARTEM 的回应
似乎 IntegrationFlow 中的一个简单转换器可以解决问题。我当前的实现似乎将消息作为Message<byte[]> 传递,而不是我期待的Message<String>。有关详细信息,请参阅下面 Artem 的完整回复。
@Bean
public IntegrationFlow flow() {
return IntegrationFlows
.from("inputChannel")
.convert(String.class)
.transform(flowService::removeLineFeeds)
.get();
}
【问题讨论】:
标签: java spring spring-integration spring-integration-dsl