当前位置: 首页 > news >正文

坑记(HttpInputMessage)

一、背景知识

public interface HttpInputMessage
extends HttpMessage
Represents an HTTP input message, consisting of headers and a readable body.Typically implemented by an HTTP request on the server-side, or a response on the client-side.Since:
3.0
Author:
Arjen Poutsma

在实现接口的加密处理过程中, 我们一般选择使用SpringMVCResponseBodyRequestBody,实现接口报文的监听和处理操作。在监听时,需分别实现相关的Advice类,以帮助完成自己的逻辑实现。交互流程图可以参考:
在这里插入图片描述

HttpInputMessage正是我们需要获取header和请求body的关键。今天我们谈谈使用过程中可能遇到的问题。

二、情况说明

1. 问题描述

我们在实现RequestBodyAdvice时,通常会重写以下方法:

 public HttpInputMessage beforeBodyRead(final HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {//读取请求bodybyte[] body = new byte[inputMessage.getBody().available()];inputMessage.getBody().read(body);
}

如我们使用以上方式,读取请求报文数据时,可能产生“读不完整”的现象。因为InputStream .available()方法描述如下:

public abstract class InputStream implements Closeable {
/*** Returns an estimate of the number of bytes that can be read (or* skipped over) from this input stream without blocking by the next* invocation of a method for this input stream. The next invocation* might be the same thread or another thread.  A single read or skip of this* many bytes will not block, but may read or skip fewer bytes.** <p> Note that while some implementations of {@code InputStream} will return* the total number of bytes in the stream, many will not.  It is* never correct to use the return value of this method to allocate* a buffer intended to hold all data in this stream.** <p> A subclass' implementation of this method may choose to throw an* {@link IOException} if this input stream has been closed by* invoking the {@link #close()} method.** <p> The {@code available} method for class {@code InputStream} always* returns {@code 0}.** <p> This method should be overridden by subclasses.** @return     an estimate of the number of bytes that can be read (or skipped*             over) from this input stream without blocking or {@code 0} when*             it reaches the end of the input stream.* @exception  IOException if an I/O error occurs.*/public int available() throws IOException {return 0;}
}

其中,有一句描述:

many bytes will not block, but may read or skip fewer bytes.

这就厉害了,不阻塞,但是可能丢包…所以如果使用上述方法,读取请求body时,数据并不完整,会导致后续逻辑出现异常。

2. 解决过程

上述方法有问题,可使用这个办法:

String reqBody = StreamUtils.copyToString(inputMessage.getBody(), Charset.defaultCharset());

亲测有效哦~

结束语

多看、多学、多试,总会找到解决的办法和途径。

http://www.lryc.cn/news/279842.html

相关文章:

  • day04打卡
  • 语义分割miou指标计算详解
  • Unity3d 实现直播功能(无需sdk接入)
  • 计算机缺失msvcr100.dll如何修复?分享五种实测靠谱的方法
  • 面试宝典进阶之redis缓存面试题
  • 调试(c语言)
  • opencv-4.8.0编译及使用
  • Jmeter 性能-监控服务器
  • Excel学习
  • 【技能---labelme软件的安装及其使用--ubuntu】
  • 回归预测 | Matlab实现SSA-CNN-LSTM-Attention麻雀优化卷积长短期记忆神经网络注意力机制多变量回归预测(SE注意力机制)
  • css垂直水平居中的几种实现方式
  • OpenHarmony之hdc
  • 【爬虫实战】-爬取微博之夜盛典评论,爬取了1.7w条数据
  • CST2024的License服务成功启动,仍报错——“The desired daemon is down...”,适用于任何版本!基础设置遗漏!
  • matlab中any()函数用法
  • Apache ECharts | 一个数据可视化图表库
  • m1 + swoole(hyperf) + yasd + phpstorm 安装和debug
  • group by 查询慢的话,如何优化?
  • 【重学C语言】一、C语言简介
  • 【MATLAB源码-第109期】基于matlab的哈里斯鹰优化算发(HHO)机器人栅格路径规划,输出做短路径图和适应度曲线。
  • NestJS 如何自定义中间件以及实际项目基于中间件提升项目开发效率
  • CMake入门教程【核心篇】设置和使用缓存变量
  • MinIO (五) .net core实现分片上传
  • 如何有效提高矢量网络分析仪的动态范围
  • Python 安卓开发:Kivy、BeeWare、Flet、Flutter
  • 50天精通Golang(第16天)
  • imx6ull基于yocto工程的l汇编点亮ed
  • vue 前端等比例压缩图片(再转换成文件后上传后端)
  • 解决在eclipse2021中,用mysql-connector-java-8.0.18.jar不兼容,导致无法访问数据库问题