StripPrefix
过滤器
StripPrefix
Filter
StripPrefix
过滤器接受一个参数 parts
。parts
参数表示在将请求发送到下游之前,要从请求路径中去掉的部分数量。以下代码片段配置了一个 StripPrefix
过滤器:
spring:
cloud:
gateway:
mvc:
routes:
- id: nameRoot
uri: https://nameservice
predicates:
- Path=/name/**
filters:
- StripPrefix=2
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.stripPrefix;
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> gatewayRouterFunctionsStripPrefix() {
return route("nameRoot")
.GET("/name/**", http("https://example.org"))
.before(stripPrefix(2))
.build();
}
}
当通过网关向 /name/blue/red
发出请求时,向 nameservice
发出的请求看起来像 [nameservice/red](https://nameservice/red)
。