PrefixPath
篩選器
PrefixPath
篩選器接受單一 prefix
參數。以下範例設定 PrefixPath
篩選器
application.yml
spring:
cloud:
gateway:
mvc:
routes:
- id: prefixpath_route
uri: https://example.org
filters:
- PrefixPath=/mypath
GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.prefixPath;
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> gatewayRouterFunctionsPrefixPath() {
return route("prefixpath_route")
.GET("/**", http("https://example.org"))
.before("/mypath")
.build();
}
}
這會在所有符合請求的路徑前加上 /mypath
前綴。因此,對 /hello
的請求會被發送到 /mypath/hello
。