SpringCloud-Gateway统一处理返回参数,代码请直接复制粘贴

说明:

SpringCloud-Gateway是基于webflux框架的(响应式编程WEB框架),就导致不能用正常的springMVC获取httpServletResponse返回值方法来统一获取返回值,所以本文就针对解决此问题
注意:
1.如果返回不是json的话(一般项目都是JSON),就不要转为json,是什么格式就用什么格式解析
2.代码中"//TODO" 标记的地方,即可对返回值进行统一处理,也可以拿到gatewayContext(gatewayContext可以看我这篇博文)
3.代码形式如此,不要纠结代码写法。reactor的思想和API使然
4.废话不多说,看代码吧

Filter代码:

import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.base.Charsets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.reactivestreams.Publisher;
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.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * @author kam
 *
 *         

* 返回内容覆盖过滤器 处理返回内容 *

*/
@Component @Slf4j public class ResponseCoverFilter implements WebFilter, Ordered { /** * 返回值处理 * * @param exchange * @param chain * @return */ @Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { ServerHttpResponse originalResponse = exchange.getResponse(); DataBufferFactory bufferFactory = originalResponse.bufferFactory(); ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) { @Override public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { if (body instanceof Flux) { if (ContentType.JSON.toString() .equals(originalResponse.getHeaders().getFirst(Header.CONTENT_TYPE.toString()))) { Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>)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); GatewayContext gatewayContext = exchange.getAttribute(GatewayContext.CACHE_GATEWAY_CONTEXT); if (Objects.nonNull(gatewayContext) && StringUtils.isNotBlank(responseData)) { //此处拿到参数转为JSONObject 处理参数内容 JSONObject jsonObject = JSON.parseObject(responseData); //TODO 对返回值JSONObject进行put remove get等操作 responseData = JSONObject.toJSONString(jsonObject, SerializerFeature.MapSortField); } byte[] uppedContent = responseData.getBytes(Charsets.UTF_8); return bufferFactory.wrap(uppedContent); })); } } return super.writeWith(body); } }; // replace response with decorator return chain.filter(exchange.mutate().response(decoratedResponse).build()); } @Override public int getOrder() { return 101; } }

希望能帮到你,有问题在留言区讨论~~

你可能感兴趣的:(gateway,SpringCloud,gateway,spring,后端,reactor,spring,cloud)