最新の安定バージョンについては、Spring Cloud Gateway 5.0.1 を使用してください! |
ModifyResponseBody フィルター
ModifyResponseBody フィルターを使用して、レスポンス本文をクライアントに送り返す前に変更できます。
| このフィルターは、JavaDSL を使用してのみ構成できます。 |
次のリストは、レスポンス本文フィルターを変更する方法を示しています。
GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.AfterFilterFunctions.modifyResponseBody;
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.host;
import org.springframework.http.MediaType;
@Configuration
class RouteConfiguration {
@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsModifyResponseBody() {
return route("modify_response_bodu")
.route(host("*.modifyresponsebodu.org"), http())
.before(uri("https://example.org"))
.after(modifyResponseBody(String.class, String.class, MediaType.APPLICATION_JSON_VALUE,
(request, s) -> s.toUpperCase()))
.build();
}
} レスポンスに本文がない場合、RewriteFilter は null に渡されます。Mono.empty() を返して、欠落している本文をレスポンスで割り当てる必要があります。 |