このバージョンはまだ開発中であり、まだ安定しているとは見なされていません。最新の安定バージョンについては、Spring Cloud Gateway 4.3.2 を使用してください!

SetPath フィルター

SetPath フィルターはパス template パラメーターを受け取ります。パスのテンプレート化されたセグメントを許可することで、リクエストパスを操作する簡単な方法を提供します。これは、Spring Framework の URI テンプレートを使用します。複数の一致するセグメントが許可されます。次の例では、SetPath フィルターを構成します。

application.yml
spring:
  cloud:
    gateway:
      mvc:
        routes:
        - id: setpath_route
          uri: https://example.org
          predicates:
          - Path=/red/{segment}
          filters:
          - SetPath=/{segment}
GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.setPath;
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;

@Configuration
class RouteConfiguration {

    @Bean
    public RouterFunction<ServerResponse> gatewayRouterFunctionsSetPath() {
        return route("setpath_route")
            .GET("/red/{segment}", http())
            .before(uri("https://example.org"))
            .before(setPath("/{segment"))
            .build();
    }
}

/red/blue のリクエストパスの場合、これにより、ダウンストリームリクエストを行う前にパスが /blue に設定されます。

lb() フィルターを使用する場合は、setPath() フィルターの後に配置する必要があります。そうしないと、結果の URL が不正確になる可能性があります。構成内の lb: スキームハンドラーは、フィルターを最も高い優先順位に自動的に配置します。