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
に設定されます。YAML 仕様のため、application.yml
では $
を $\
に置き換える必要があることに注意してください。