PrefixPath Filter

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