使用标头在HTTP出站通道适配器中构建URL



我正在用像这样的豆类使用兔子队列的消息:

IntegrationFlows.from(Amqp.inboundGateway(listenerContainer()).errorChannel(FailedFlow.CHANNEL_NAME))
            .transform(Transformers.fromJson())
            .channel(TestFlow.CHANNEL_NAME)
            .get();

该消息会贯穿一些路由器和标头富集,最终以类似的bean配置的豆类流到出站流中:

IntegrationFlows.from(CHANNEL_NAME)
            .transform(Transformers.toJson())
            .handle(Http.outboundChannelAdapter("http://localhost:8080/api/test")
                            .httpMethod(HttpMethod.POST)
                            .requestFactory(getRequestFactory()))
            .get();

这在测试时正常工作,但是为了实际使用,我需要使用存储在标头中的基本URL将请求发送给不同的服务器。

IntegrationFlows.from(CHANNEL_NAME)
            .transform(Transformers.toJson())
            .handle(e -> Http.outboundChannelAdapter(String.format("%s/test",
                    e.getHeaders().get(IntegrationConstants.NOTIFY_BASE_URL_HEADER_NAME)))
            .httpMethod(HttpMethod.POST)
            .requestFactory(getRequestFactory()))
            .get();

看来,在测试配置中,我将MessageHandlersPec传递到句柄方法,在实际配置中,我将MessageHandler传递给句柄方法。我不确定有什么区别,我所知道的是,传递MessageHandler时未调用端点。

在维护MessageHandlersPec的直接使用时,如何访问标题?

.handle(IntegrationFlows.from(CHANNEL_NAME)
                    .transform(Transformers.toJson())
                    .handle(Http.outboundChannelAdapter("{baseUrl}/test")
                            .httpMethod(HttpMethod.POST)
                            .requestFactory(getRequestFactory())
                            .uriVariable("baseUrl", e -> e.getHeaders().get(IntegrationConstants.NOTIFY_BASE_URL_HEADER_NAME)))
                    .get())

:-D

最新更新