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