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);
	}

}
相关推荐
汤永红几秒前
windows下git bash安装SDKMan报错Looking for unzip...Not found
java·git·sdkman
云惠科技(SEO)20 分钟前
泛目录站群技术架构演进观察:2025年PHP+Java混合方案实战笔记
java·人工智能·搜索引擎
牛马baby1 小时前
Springboot 自动装配原理是什么?SPI 原理又是什么?
java·spring boot·后端
小小深1 小时前
了解JVM
java·jvm
Sunlight_7771 小时前
第五章 SQLite数据库:1、SQLite 基础语法及使用案例
java·linux·服务器·jvm·数据库·tcp/ip·sqlite
JhonKI2 小时前
【从零实现高并发内存池】内存池整体框架设计 及 thread cache实现
java·redis·缓存
何似在人间5752 小时前
SpringAI+DeepSeek大模型应用开发——4 对话机器人
java·机器人·大模型应用开发·spring ai
-曾牛2 小时前
【LangChain4j快速入门】5分钟用Java玩转GPT-4o-mini,Spring Boot整合实战!| 附源码
java·开发语言·人工智能·spring boot·ai·chatgpt
kfepiza2 小时前
HttpSessionListener 的用法笔记250417
java·笔记·servlet·tomcat
冬天vs不冷2 小时前
SpringBoot条件注解全解析:核心作用与使用场景详解
java·spring boot·python