zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    linux 静态库和动态库(共享库)的制作与使用(注意覆盖问题)转
    手机号码格式正则表达式
    项目去除TFS关联、迁移重部署
    Excel中VLOOKUP函数的用法和注意点
    自定义打赏插件
    分享一个无需注册,无次数限制的Smile聊天机器人接口
    TCP/IP
    Java字典树
    平衡二叉树结构 AVL
    二叉搜索树(二叉排序树)BST
  • 原文地址:https://www.cnblogs.com/zhouquan-1992-04-06/p/15558044.html
Copyright © 2011-2022 走看看