[英]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.