SetPath
过滤器
SetPath
Filter
SetPath
过滤器接受一个路径 template
参数。它提供了一种简单的方式来操作请求路径,允许使用路径的模板化片段。它使用了 Spring Framework 中的 URI 模板。允许多个匹配片段。以下示例配置了一个 SetPath
过滤器:
spring:
cloud:
gateway:
mvc:
routes:
- id: setpath_route
uri: https://example.org
predicates:
- Path=/red/{segment}
filters:
- SetPath=/{segment}
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
。