网上找到一段获取修改response的代码:https://blog.csdn.net/tianyaleixiaowu/article/details/83618037

代码如下:

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.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; import java.nio.charset.Charset; /**
* @author wuweifeng wrote on 2018/10/31.
*/
@Component
public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered {
@Override
public int getOrder() {
// -1 is response write filter, must be called before that
return -2;
} @Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain 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) {
Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>) body;
return super.writeWith(fluxBody.map(dataBuffer -> {
// probably should reuse buffers
byte[] content = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(content);
//释放掉内存
DataBufferUtils.release(dataBuffer);
String s = new String(content, Charset.forName("UTF-8"));
//TODO,s就是response的值,想修改、查看就随意而为了
byte[] uppedContent = new String(content, Charset.forName("UTF-8")).getBytes();
return bufferFactory.wrap(uppedContent);
}));
}
// if body is not a flux. never got there.
return super.writeWith(body);
}
};
// replace response with decorator
return chain.filter(exchange.mutate().response(decoratedResponse).build());
}
}

实践后发现,在多次测试或者压力测试时,会间歇性出现问题。

问题在这里

  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 -> {
// probably should reuse buffers
byte[] content = new byte[dataBuffer.readableByteCount()];

这段代码中,重写了response的构造方法,将body映射为需要的byte[]以便于后面操作

但是这里的dataBuffer经常取到的长度低于实际长度,丢失了内容。

还没找到内容,先换一种方法实现,待续...

spring gateway 截取response 长度缺失的更多相关文章

  1. spring cloud gateway获取response body

    网关发起请求后,微服务返回的response的值要经过网关才发给客户端.本文主要讲解在spring cloud gateway 的过滤器中获取微服务的返回值,因为很多情况我们需要对这个返回进行处理.网 ...

  2. asp.net截取指定长度的字符串内容

    /// <summary> /// 用于截取指定长度的字符串内容 /// </summary> /// <param name="sString"&g ...

  3. .net截取指定长度汉字超出部分以指定的字符代替

    下面是我在网上搜索,然后加以整理的关于在.net中截取指定长度汉字超出部分以指定的字符代替,来拓展一下自己的思路. 方法一 :在后台的select语句中直接操作或是在数据库中写一个存储过程 Selec ...

  4. js实现超过长度的字符截取指定长度(中文字符算2个字符),超出部分以...显示

    //超过长度的字符截取指定长度,超出部分以...显示 function subString(str, len) { var newLength = 0; var newStr = "&quo ...

  5. 解决 ASP.NET Core MySql varchar 字符串截取(长度 255)

    ASP.NET Core 中使用 MySql,如果字段类型为varchar,不管设置多少长度,插入或更新数据的时候,会自动截断(截取 255 长度的字符). 出现问题的原因,就是使用了MySql.Da ...

  6. DEDE中如何过滤掉Html标签,并且截取字符串长度

    在dede标签中只要使用2个函数就可以. [field:body function="cn_substr(Html2text(@me),80)"/] Html2text()函数是去 ...

  7. sql中从指定位置截取指定长度字符串

    1. 字符串函数应用 --从指定索引截取指定长度的字符串 ,) --获取字符串中指定字符的索引(从1开始) select charindex(',','ab,cdefg') --实际应用中的语句 , ...

  8. Oracle 截取指定长度的字符

    去掉回车,换行符号,截取指定长度的字符 具体代码示例: --Function --去掉前后空格,截取字符,字符长度为P_Length create or replace function get_St ...

  9. 用C#截取指定长度的中英文混合字符串

    很早以前写过一篇文章(用C#截取指定长度的中英文混合字符串),但是对性能没有测试,有人说我写的这个方法性能有问题,后来想,可能真会有BT之需求要求传入一个几万K甚至几M体积的字符串进来,那将会影响正则 ...

随机推荐

  1. C#多线程和线程池问题

    static void Main(string[] args) { Thread threadA = new Thread(ThreadMethod); //执行的必须是无返回值的方法 threadA ...

  2. 在Android中调用USB摄像头

    在网上找了很长时间,网上的资料基本都是说用外国人写的库,但实际上这个库的案例都是不能直接用的(因为权限问题),并不适合学习. 之后偶然发现有国人把这个库重新封装了,并且有源代码以及中文教程: http ...

  3. 3.27模拟赛 sutoringu(后缀数组)

    \(\color{white}{mjt是机房模拟赛独自切过题的唯一的人...}\) (应本人要求删掉惹) \(Description\) 给你\(n,k\)和长为\(n\)的字符串\(s\).一个区间 ...

  4. BZOJ.4052.[Cerc2013]Magical GCD(思路)

    BZOJ \(Description\) 给定\(n\)个数的序列\(a_i\).求所有连续子序列中,序列长度 × 该序列中所有数的gcd 的最大值. \(n\leq10^5,\ a_i\leq10^ ...

  5. [CF528D]Fuzzy Seach

    Description: 有两个基因串S和T,他们只包含AGCT四种字符.现在你要找出T在S中出现了几次. 有一个门限值k≥0.T在S的第i(1≤i≤|S|-|T|+1)个位置中出现的条件如下:把T的 ...

  6. pygame-KidsCanCode系列jumpy-part15-PowerUp加速器

    这一节我们给游戏增加点额外的奖励,大多数游戏中都会有金币.装备啥的来激励玩家,在jumpy这个游戏中,我们也可以增加类似的道具:加速器.效果图如下: 档板上会随机出现一些加速器(power up),小 ...

  7. spring注解之@profile

    spring中@profile与maven中的profile很相似,通过配置来改变参数. 例如在开发环境与生产环境使用不同的参数,可以配置两套配置文件,通过@profile来激活需要的环境,但维护两套 ...

  8. ES5, ES6, ES2016, ES.Next: What's going on with JavaScript versioning?

    JavaScript has a strange naming history. For its initial release in 1995 as part of Netscape Navigat ...

  9. 对Unity的Resources目录进行改名

    项目用的是Unity5.5版本,开发的时候将相关的图集.预制对象资源都放在 Resources 目录下,而真机使用的是 StreamingAssets 目录下的资源. Resources(不分层级)在 ...

  10. 微软 microsoft calendar control 11.0 控件下载

    微软 microsoft calendar control  11.0 控件下载 https://files.cnblogs.com/files/mqingqing123/csccal2.rar