【发布时间】:2019-02-13 06:00:22
【问题描述】:
我正在尝试使用与 @Bean 定义混合的 DSL 样式流来设置 Spring 集成流。在此示例中,我尝试将传入的 REST 请求 (restCustomerGateway) 作为入站 webflux 网关进行调解。我看到您可以使用 .payloadExpression 将内容从请求中提取出来(在这种情况下,id 路径参数...如果有更好或更多类型安全的方法会很有趣)。
然后,我将其流入 webflex 出站网关 (restCustomerSource) 以进行下游调用,然后应将其作为响应汇回入站网关。请注意,最终会有转换器在两者之间进行有效负载转换/等。
简单的问题是,我如何构造它以便我可以访问“id”(路径参数,当前在出站网关调用中硬编码为“1”)?我假设这是在两者之间流动的消息有效负载的一部分,但我如何获得它的句柄?
@Bean
public WebFluxInboundEndpoint restCustomerGateway() {
return WebFlux.inboundGateway("/rest/customers/{id}")
.requestMapping(m -> m.produces(MediaType.APPLICATION_JSON_VALUE)).payloadExpression("#pathVariables.id")
.get();
}
@Bean
public WebFluxRequestExecutingMessageHandler restCustomerSource() {
return WebFlux.outboundGateway("http://localhost:8080/customers/1").httpMethod(HttpMethod.GET)
.expectedResponseType(Customer.class)
.get();
}
@Bean
public IntegrationFlow restCustomerFlow(CustomerProcessor customerProcessor) {
return IntegrationFlows
.from(restCustomerGateway())
.handle(restCustomerSource())
.handle(customerProcessor)
.get();
}
【问题讨论】: