+-
仅对某些内容类型应用modifyResponseBody

我使用GatewayFilterSpec.modifyResponseBody(标记为“BETA”功能)来重写JSON有效负载。只要响应有效负载实际上是内容类型application/json,这就可以正常工作。在我的情况下,遗憾的是并不总能保证,如果响应有modifyResponseBody标题,我希望它只应用Content-Type: application/json,否则跳过过滤器。这可能是Spring Cloud Gateway,以及如何做到这一点?谢谢。

现在我明白了:

org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'text/html' not supported
    at org.springframework.web.reactive.function.BodyInserters.lambda$null$11(BodyInserters.java:329)
    at java.util.Optional.orElseGet(Optional.java:267)
    at org.springframework.web.reactive.function.BodyInserters.lambda$bodyInserterFor$12(BodyInserters.java:325)
0
投票

这是一个“解决方案”,有各种各样的问题:

package my_package;

import org.reactivestreams.Publisher;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.rewrite.ModifyResponseBodyGatewayFilterFactory;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import static org.springframework.http.MediaType.APPLICATION_JSON;

@Component
@Primary
public class JsonOnlyModifyResponseBodyGatewayFilterFactory extends ModifyResponseBodyGatewayFilterFactory {
    public JsonOnlyModifyResponseBodyGatewayFilterFactory(ServerCodecConfigurer codecConfigurer) {
        super(codecConfigurer);
    }

    @Override
    public GatewayFilter apply(Config config) {
        return new MyModifyResponseGatewayFilter(config);
    }

    public class MyModifyResponseGatewayFilter extends ModifyResponseGatewayFilter {
        MyModifyResponseGatewayFilter(Config config) {
            super(config);
        }

        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            ServerHttpResponse serverHttpResponse = getServerHttpResponseFromSuper(exchange);
            ServerHttpResponseDecorator responseDecorator = new ServerHttpResponseDecorator(exchange.getResponse()) {
                @Override
                public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
                    if (APPLICATION_JSON.isCompatibleWith(getDelegate().getHeaders().getContentType())) {
                        return serverHttpResponse.writeWith(body);
                    }
                    return super.writeWith(body);
                }
            };
            return chain.filter(exchange.mutate().response(responseDecorator).build());
        }

        private ServerHttpResponse getServerHttpResponseFromSuper(ServerWebExchange exchange) {
            ServerHttpResponse[] serverHttpResponse = new ServerHttpResponse[1];
            //noinspection UnassignedFluxMonoInstance
            super.filter(exchange, chain -> {
                serverHttpResponse[0] = chain.getResponse(); // capture the response when the super sets it
                return null;
            });
            return serverHttpResponse[0];
        }
    }
}

选择的方法不仅仅是更改现有ModifyResponseBodyGatewayFilterFactory的副本。这允许Spring Boot Gateway的版本升级带来ModifyResponseBodyGatewayFilterFactory的微小变化。但由于JsonOnlyModifyResponseBodyGatewayFilterFactory非常依赖ModifyResponseBodyGatewayFilterFactory的实施,这可能很容易被打破。这个解决方案的另一个缺陷是我必须放置一个@Primary注释以避免required a single bean, but 2 were found异常,但它会覆盖默认值,这可能会影响modifyResponseBody的其他用途。调用super.filter而不是使用它的结果是很难看的。等等。所以,虽然这“有效”,但它并没有让我充满喜悦。