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.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;

@Configuration
class RouteConfiguration {

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

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