I'm building REST services using spring-mvc and what I'm looking for now is a way to proxy HTTP request to external REST service from inside Spring MVC controller.
I'm getting HttpServletRequest object and want to proxy it making as few changes as possible. What is essential for me is keeping all the headers and attributes of incoming request as they are.
@RequestMapping('/gateway/**')
def proxy(HttpServletRequest httpRequest) {
...
}
I was trying simply to send another HTTP request to external resource using RestTemplate but I failed to find a way to copy REQUEST ATTRIBUTES (which is very important in my case).
Thanks in advance!
You can use the spring rest template method exchange to proxy the request to a third party service.
What is the restTemplate.exchange() method for?
if you think of applying the API gateway pattern for microservices, have a look at Netflix zuul which is a good alternative in the spring boot ecosystem. A good example is provided here.
I wrote this ProxyController method in Kotlin to forward all incoming requests to remote service (defined by host and port) as follows:
Note that the API of the remote service should be exactly same as this service.