spring cloud gateway 修改response 截断问题,乱码问题解决

              Spring cloud gateway 处理跨域问题

               Spring cloud gateway 设置https 和http同时支持

              Spring cloud gateway 拦截请求404 等HTTP 状态码

              Spring cloud gateway 详解和配置使用(文章较长) 

              Spring cloud Gateway 指定执行过滤器 (在配置文件中配置所需要过滤器)

乱码问题其实就是返回数据gateway 进行了截断所产生的,所以我们需要把返回的组进行join上就不会产生乱码的,如果还是乱码,请根据自己系统的实际编码进行转换即可,完美解决,

请注意:这篇文章不同于以往文章这里使用的是 DefaultDataBufferFactory 的join方法去合并多个dataBuffers ↓↓↓↓↓↓↓↓↓↓↓↓↓↓ 最大的区别

  DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
  DataBuffer join = dataBufferFactory.join(dataBuffers);
  byte[] content = new byte[join.readableByteCount()];
  join.read(content);
  DataBufferUtils.release(join);
  String responseData = new String(content, StandardCharsets.UTF_8);

下面是详细代码

import com.google.common.base.Charsets;
import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Publisher;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpStatus;
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.Flux;
import reactor.core.publisher.Mono;

@Component
@Slf4j
public class DangDeleDangDangYiDangDangFilter implements GlobalFilter, Ordered {
    @Override
    public int getOrder() {
        //-1 is response write filter, must be called before that
        return -2;
    }

    @Override
    public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        try {
            //获取response的 返回数据
            ServerHttpResponse originalResponse = exchange.getResponse();
            DataBufferFactory bufferFactory = originalResponse.bufferFactory();
            HttpStatus statusCode = originalResponse.getStatusCode();

            if (statusCode == HttpStatus.OK) {
                ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) {
                    @Override
                    public Mono writeWith(Publisher body) {
                    // 判断服务返回的数据类型进行拦截,根据自己的业务进行修改
                        if (APPLICATION_JSON.isCompatibleWith(getDelegate().getHeaders().getContentType())) {
                            Flux fluxBody = Flux.from(body);
                            return super.writeWith(fluxBody.buffer().map(dataBuffers -> {
                                DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
                                DataBuffer join = dataBufferFactory.join(dataBuffers);
                                byte[] content = new byte[join.readableByteCount()];
                                join.read(content);
                                DataBufferUtils.release(join);
                                String responseData = new String(content, Charsets.UTF_8);
                                if (log.isDebugEnabled()) {
                                    log.debug("响应转前:{}", responseData);
                                }
                                responseData = responseData.replaceAll(":null", ":\"\"");
                                if (log.isDebugEnabled()) {
                                    log.debug("响应转后:{}", responseData);
                                }
                                byte[] uppedContent = responseData.getBytes(Charsets.UTF_8);
                                 
                                return bufferFactory.wrap(uppedContent);
                            }));
                        } else {
                            return chain.filter(exchange);
                        }
                    }
                };
                return chain.filter(exchange.mutate().response(decoratedResponse).build());
            }
            return chain.filter(exchange);
        } catch (Exception e) {
            log.error(" ReplaceNullFilter 异常", e);
            return chain.filter(exchange);
        }
    }
}

 

 

完美解决了

 

你可能感兴趣的:(spring,cloud,gateway,修改gateway,响应截断问题,修改gateway响应,返回乱码问题,spring,cloud,gateway,响应的response)