RewritePath
篩選器
RewritePath
篩選器接受路徑 regexp
參數和 replacement
參數。這使用 Java 正則表達式,以彈性的方式來重寫請求路徑。以下列表設定了 RewritePath
篩選器
application.yml
spring:
cloud:
gateway:
mvc:
routes:
- id: rewritepath_route
uri: https://example.org
predicates:
- Path=/red/**
filters:
- RewritePath=/red/?(?<segment>.*), /$\{segment}
GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.rewritePath;
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> gatewayRouterFunctionsRewritePath() {
return route("rewritepath_route")
.GET("/red/**", http("https://example.org"))
.before(rewritePath("/red/(?<segment>.*)", "/${segment}"))
.build();
}
}
對於 /red/blue
的請求路徑,這會在發出下游請求之前將路徑設定為 /blue
。請注意,在 application.yml
中,由於 YAML 規範,$
應替換為 $\
。