spring-cloud/spring-cloud-gateway

Support paths on Route URIs as syntactic sugar for the `SetPath` filter

Open

#3293 opened on Mar 11, 2024

View on GitHub
 (0 comments) (0 reactions) (0 assignees)Java (4,284 stars) (3,204 forks)batch import
enhancementhelp wanted

Description

There have been multiple requests and feedback regarding the ignoring of paths on Route URIs. Part of my hesitancy is how to support it across ALL means of configuring routes both reactive and MVC: config props, Java DSL and actuator.

I'm comfortable proposing it as syntactic sugar for the SetPath filter. There would be a mechanism to remove the path from the URI and insert a SetPath filter with that URI. There would be NO customization, at that point, break out to the original SetPath configuration.

The two routes in each of the following examples would be equivalent:

spring:
  cloud:
    gateway:
      routes:
      - id: set_path
        uri: http://example.com
        predicates:
        - Path=/foo/{segment}
        filters:
        - SetPath=/{segment}
      - id: set_path_uri
        uri: http://example.com/{segment}
        predicates:
        - Path=/foo/{segment}

reactive Java DSL

@Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
  return builder.routes()
      .route("setpath", p -> p.path("/foo/{segment}")
          .filters(f -> f.setPath("/{segment}"))
          .uri("http://example.com"))
      .route("setpathuri", p -> p.path("/foo/{segment}")
          .uri("http://example.com/{segment}"))
      .build();
}

MVC Java DSL

@Bean
public RouterFunction<ServerResponse> setPathRoute() {
  return route("setpath")
      .route(path("/foo/{segment}"), http("http://example.com"))
        .before(setPath("/{segment}"))
      .build().and(route()
        .route(path("/foo/{segment}"), http("http://example.com/{segment}"))
      .build());
}

/cc @joshlong

Contributor guide