跳到主要内容

StripPrefix 过滤器

DeepSeek V3 中英对照 StripPrefix Filter StripPrefix Filter

StripPrefix 过滤器接受一个参数 partsparts 参数表示在将请求发送到下游之前,要从请求路径中去掉的部分数量。以下代码片段配置了一个 StripPrefix 过滤器:

spring:
cloud:
gateway:
mvc:
routes:
- id: nameRoot
uri: https://nameservice
predicates:
- Path=/name/**
filters:
- StripPrefix=2
yaml
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();
}
}
java

当通过网关向 /name/blue/red 发出请求时,向 nameservice 发出的请求看起来像 [nameservice/red](https://nameservice/red)