ModifyRequestBody GatewayFilter Factory

你可以使用 ModifyRequestBody 筛选器,在网关下游发送之前修改请求体。

You can use the ModifyRequestBody filter to modify the request body before it is sent downstream by the gateway.

此过滤器只能使用 Java DSL 来配置。

This filter can be configured only by using the Java DSL.

以下清单显示如何修改请求体 GatewayFilter

The following listing shows how to modify a request body GatewayFilter:

@Bean
public RouteLocator routes(RouteLocatorBuilder builder) {
    return builder.routes()
        .route("rewrite_request_obj", r -> r.host("*.rewriterequestobj.org")
            .filters(f -> f.prefixPath("/httpbin")
                .modifyRequestBody(String.class, Hello.class, MediaType.APPLICATION_JSON_VALUE,
                    (exchange, s) -> Mono.just(new Hello(s.toUpperCase())))).uri(uri))
        .build();
}

static class Hello {
    String message;

    public Hello() { }

    public Hello(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

如果该请求没有正文,则会传递 RewriteFilter。应该返回 Mono.empty() 以在请求中分配一个缺失的正文。

If the request has no body, the RewriteFilter is passed null. Mono.empty() should be returned to assign a missing body in the request.