简体   繁体   English

为每条消息的 HttpRequestExecutingMessageHandler 配置 RestTemplate

[英]Configure RestTemplate for HttpRequestExecutingMessageHandler per message

I am working on a spring integration project where I am creating outbound gateway using HttpRequestExecutingMessageHandler class.我正在开发一个 spring 集成项目,我正在使用HttpRequestExecutingMessageHandler类创建出站网关。 I want to execute different APIs which are authenticated using bearer authentication tokens using single gateway.我想执行使用单个网关使用承载身份验证令牌进行身份验证的不同 API。 I have different RestTemplate configured to handle each type of message.我配置了不同的 RestTemplate 来处理每种类型的消息。

I could not find how to configure restTemplate per message basis using this class.我找不到如何使用此类为每个消息配置 restTemplate。 This class only accepts restTemplate at construction time.此类仅在构建时接受 restTemplate。 https://docs.spring.io/spring-integration/api/org/springframework/integration/http/outbound/HttpRequestExecutingMessageHandler.html https://docs.spring.io/spring-integration/api/org/springframework/integration/http/outbound/HttpRequestExecutingMessageHandler.html

For now, I have created custom gateway class by extended AbstractHttpRequestExecutingMessageHandler which is parent of HttpRequestExecutingMessageHandler and copied contents of exchange method from HttpRequestExecutingMessageHandler class.现在,我通过扩展AbstractHttpRequestExecutingMessageHandler创建了自定义网关类,它是 HttpRequestExecutingMessageHandler 的父级,并从 HttpRequestExecutingMessageHandler 类中复制了交换方法的内容。 I am passing restTemplate bean name as message header and resolving it at the start of exchange method.我将 restTemplate bean 名称作为消息头传递并在交换方法开始时解析它。

Is there any better approach?有没有更好的办法?

The "using single gateway" is already a wrong decision from design. “使用单一网关”已经是设计上的错误决定。

You probably need to think about having different HttpRequestExecutingMessageHandler for those purposes.您可能需要考虑为这些目的使用不同的HttpRequestExecutingMessageHandler In your logic you can add then a router to decide to which HTTP Outbound Gateway to send a message.在您的逻辑中,您可以添加一个路由器来决定向哪个 HTTP 出站网关发送消息。

See more info in docs: https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/message-routing.html#messaging-routing-chapter在文档中查看更多信息: https : //docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/message-routing.html#messaging-routing-chapter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pulsar 消息总线:我们可以为每个主题配置消息保留吗? - Pulsar message bus: Can we configure message retention per topic? 如何使用RestTemplate为每个请求设置RequestConfiguration? - How to set RequestConfiguration per request using RestTemplate? 使用MockRestServiceServer测试HttpRequestExecutingMessageHandler - Testing HttpRequestExecutingMessageHandler using MockRestServiceServer 将ParameterizedTypeReference用于HttpRequestExecutingMessageHandler的setExpectedResponseType - Using ParameterizedTypeReference to setExpectedResponseType for HttpRequestExecutingMessageHandler 发布时Spring RestTemplate消息转换器优先级 - Spring RestTemplate message converter priority when posting RestTemplate 响应中的错误消息始终为“null” - Error message always "null" from RestTemplate response 如何为 Spring 中的每个客户端配置和构建自定义 RestTemplate? - How to configure and build a custom RestTemplate for each client in Spring? 将HttpRequestExecutingMessageHandler的ExpectedResponseType设置为Page - Set the HttpRequestExecutingMessageHandler's expectedResponseType to a Page 如何为客户端和服务器配置Spring Boot RestTemplate代理 - How to configure spring boot resttemplate proxy for client and server 使用RestTemplate时出现消息“不支持HTTP协议”的异常 - Getting Exception with message “http protocol is not supported” when using RestTemplate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM