StripPrefix
篩選器
StripPrefix
篩選器接受一個參數,parts
。parts
參數指示在將請求向下游發送之前,要從路徑中剝離的部分數量。以下列表設定了 StripPrefix
篩選器
application.yml
spring:
cloud:
gateway:
mvc:
routes:
- id: nameRoot
uri: https://nameservice
predicates:
- Path=/name/**
filters:
- StripPrefix=2
GatewaySampleApplication.java
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
。