I am working on a spring integration project where I am creating outbound gateway using HttpRequestExecutingMessageHandler class. I want to execute different APIs which are authenticated using bearer authentication tokens using single gateway. I have different RestTemplate configured to handle each type of message.
I could not find how to configure restTemplate per message basis using this class. This class only accepts restTemplate at construction time. 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. I am passing restTemplate bean name as message header and resolving it at the start of exchange method.
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. In your logic you can add then a router to decide to which HTTP Outbound Gateway to send a message.
See more info in docs: https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/message-routing.html#messaging-routing-chapter