Netty HttpServerCodec和HttpObjectAggregator

GET请求

Get请求包括两个部分:

request line(包括method,request uri,protocol version))

header

HttpServerCodec是netty针对http编解码的处理类,但是这些只能处理像http get的请求,也就是数据带在url问号后面的http请求参数

POST请求

POST请求包括三个部分

request line(包括method,request uri,protocol version))

header

message body

从上可以看出,当我们用POST方式请求服务器的时候,对应的参数信息是保存在message body中的,如果只是单纯的用HttpServerCodec是无法完全的解析Http POST请求的,因为HttpServerCodec只能获取uri中参数,所以需要加上HttpObjectAggregator。

HttpObjectAggregator这个netty的处理器就是为了解决这个问题而来的.它把HttpMessage和HttpContent聚合成为一个FullHttpRquest或者FullHttpRsponse,大致结构如下图所示:

复制代码
public class ChannelHandlerInitializer extends ChannelInitializer<SocketChannel> {

	@Override
	protected void initChannel(SocketChannel ch) throws Exception {

		// HTTP编码解码器
		ch.pipeline().addLast("http-codec", new HttpServerCodec());
		// 把HTTP头、HTTP体拼成完整的HTTP请求
		ch.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
        // httpRequestHandler 中获取到的就是一个完整的Http消息了
		ch.pipeline().addLast("http-handler", httpRequestHandler);
	}

}
相关推荐
_码农121389 分钟前
模拟tomcat接收GET、POST请求
java·tomcat
板板正1 小时前
SpringAI——向量存储(vector store)
java·spring boot·ai
野生技术架构师1 小时前
Spring Boot 定时任务与 xxl-job 灵活切换方案
java·spring boot·后端
苹果醋32 小时前
Java并发编程-Java内存模型(JMM)
java·运维·spring boot·mysql·nginx
你怎么知道我是队长2 小时前
C语言---编译的最小单位---令牌(Token)
java·c语言·前端
Elieal3 小时前
Java 链表完全指南:从基础到力扣简单题实战
java·leetcode·链表
寒士obj3 小时前
SpringBoot中的条件注解
java·spring boot·后端
pengzhuofan3 小时前
Java设计模式-外观模式
java·设计模式·外观模式
Emrys_3 小时前
AQS 深入解析
java
超级小忍4 小时前
从零开始:JDK 在 Windows、macOS 和 Linux 上的下载、安装与环境变量配置
java·windows·macos