spring gateway 重写 输出不起作用

@Bean
    @Order(-100000)
    public GlobalFilter modifyResponseFilter() {
        return (exchange, chain) -> {
            ServerHttpResponse originalResponse = exchange.getResponse();
            DataBufferFactory bufferFactory = originalResponse.bufferFactory();
            ServerHttpResponseDecorator responseDecorator = new ServerHttpResponseDecorator(originalResponse) {
                @Override
                public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
                    if (body instanceof Flux) {
                        Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>)body;
                        return super.writeWith(fluxBody.map(dataBuffer -> {
                            byte[] buff = new byte[dataBuffer.readableByteCount()];
                            dataBuffer.read(buff);
                            String str = new String(buff);
                            ApiResult apiResult = new ApiResult();
                            apiResult.setS(originalResponse.getRawStatusCode());
                            apiResult.setR(JSONUtil.parseObj(str));
                            byte[] newBuff = JSONUtil.toJsonStr(apiResult).getBytes(StandardCharsets.UTF_8);
                            return bufferFactory.wrap(newBuff);
                        }));
                    }
                    return super.writeWith(body);
                }
            };
            return chain.filter(exchange.mutate().response(responseDecorator).build());
        };
    }

  如上代码在配置类里面定义了一个bean,路由的接口会走filter方法但不会走writeWith,原因是@Order(-100000)不起作用,把上面代码改成如下就没问题了

@Component
public class GlobalModifyResponseFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpResponse originalResponse = exchange.getResponse();
        DataBufferFactory bufferFactory = originalResponse.bufferFactory();
        ServerHttpResponseDecorator responseDecorator = new ServerHttpResponseDecorator(originalResponse) {
            @Override
            public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
                if (body instanceof Flux) {
                    Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>)body;
                    return super.writeWith(fluxBody.map(dataBuffer -> {
                        byte[] buff = new byte[dataBuffer.readableByteCount()];
                        dataBuffer.read(buff);
                        String str = new String(buff);
                        byte[] newBuff = JSONUtil.toJsonStr(ApiResultVO.success(str)).getBytes(StandardCharsets.UTF_8);
                        return bufferFactory.wrap(newBuff);
                    }));
                }
                return super.writeWith(body);
            }
        };
        return chain.filter(exchange.mutate().response(responseDecorator).build());
    }

    @Override
    public int getOrder() {
        return Integer.MIN_VALUE;
    }
}

 

posted @ 2021-11-15 20:14  风的低吟  阅读(1228)  评论(0编辑  收藏  举报