PrefixPath フィルター

PrefixPath フィルターは 1 つの prefix パラメーターを取ります。次の例では、PrefixPath フィルターを構成します。

application.yml
spring:
  cloud:
    gateway:
      mvc:
        routes:
        - id: prefixpath_route
          uri: https://example.org
          predicates:
          - Path=/**
          filters:
          - PrefixPath=/mypath
GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.prefixPath;
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> gatewayRouterFunctionsPrefixPath() {
        return route("prefixpath_route")
            .GET("/**", http())
            .before(uri("https://example.org"))
            .before(prefixPath("/mypath"))
            .build();
    }
}

これにより、一致するすべてのリクエストのパスに /mypath のプレフィックスが付けられます。/hello へのリクエストは /mypath/hello に送信されます。

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