坑记(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 {
   
      //读取请求body
      byte[] 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());

亲测有效哦~

结束语

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

相关推荐

  1. Python 踩

    2024-01-11 06:26:01       16 阅读
  2. Flutter踩之二

    2024-01-11 06:26:01       28 阅读
  3. Go语言学习踩

    2024-01-11 06:26:01       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-11 06:26:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-11 06:26:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-11 06:26:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-11 06:26:01       20 阅读

热门阅读

  1. 第二百五十七回

    2024-01-11 06:26:01       34 阅读
  2. Git常用命令和QA(网摘)

    2024-01-11 06:26:01       31 阅读
  3. Git基础指令4.0

    2024-01-11 06:26:01       35 阅读
  4. docker-compose

    2024-01-11 06:26:01       33 阅读
  5. hive sql 和 spark sql的区别

    2024-01-11 06:26:01       27 阅读
  6. GoLang:sql.Exec()报错

    2024-01-11 06:26:01       36 阅读
  7. 使用Spring Boot集成中间件:基础篇

    2024-01-11 06:26:01       30 阅读
  8. Linux踢掉远程用户

    2024-01-11 06:26:01       34 阅读
  9. arm64上面运行armhf程序

    2024-01-11 06:26:01       39 阅读
  10. Hadoop之mapreduce参数大全-5

    2024-01-11 06:26:01       27 阅读
  11. Hadoop之mapreduce参数大全-6

    2024-01-11 06:26:01       31 阅读
  12. windows配置电脑网络ip地加的方法

    2024-01-11 06:26:01       36 阅读
  13. adb forward使用

    2024-01-11 06:26:01       44 阅读