Reputation: 24676
I'm starting a new Spring Boot project using Spring Cloud Gateway: I need to implement a proxy application for an existing REST API.
The proxy application will implement new features (endpoints) while forwarding to the "old" application all the requests sent to existing endoints.
(Then I'll gradually move also the existing endpoints to the new application, following an approach similar to Strangler Pattern)
But I also need to rewrite the path of several existing endpoints, something like:
return routeLocatorBuilder.routes()
.route(p -> p
.path("/new-endopint")
.map("/old-endpoint") // <= is there something like 'map' method?
.uri("http://old-app-url")).build();
Is this possible? Is there some way to map an endpoint to another?
Upvotes: 1
Views: 4967
Reputation: 5309
In cloud gateway there is a org.springframework.cloud.gateway.route.RouteDefinition
that can map an incoming request to an upstream by applying FilterDefinition
and PredicateDefinition
.
You can see how this works by having a look at org.springframework.cloud.gateway.discovery.DiscoveryClientRouteDefinitionLocator
.
So a simple RouteDefinitionLocator
e.g. InMemoryRouteDefinitionRepository
could solve your use case.
If you want to stay with the high level api, then org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder.RouteSpec#predicateBuilder
(org.springframework.cloud.gateway.route.builder.GatewayFilterSpec#rewritePath
...) looks promising.
return routeLocatorBuilder.routes()
.routes()
.route(
p ->
p.path("/new-endpoint/**")
.filters(
spec ->
spec.rewritePath(
"/new-endpoint/(?<segment>.*)", "/old-endpoint/${segment}"))
.uri("http://old-app-url"))
.build();
Upvotes: 2