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

}
相关推荐
choice of13 分钟前
SpringMVC通过注解实现全局异常处理
java·后端·spring
单线程bug13 分钟前
Spring Boot中Filter与Interceptor的区别
java·spring boot·后端
小蒜学长19 分钟前
基于uni-app的蛋糕订购小程序的设计与实现(代码+数据库+LW)
java·数据库·spring boot·后端·小程序·uni-app
GitCode官方21 分钟前
告别环境地狱!Java生态“AI原生”解决方案入驻 GitCode
java·开源·gitcode
东方芷兰1 小时前
Leetcode 刷题记录 21 —— 技巧
java·算法·leetcode·职场和发展·github·idea
kyle~2 小时前
排序---选择排序(Selection Sort)
java·算法·排序算法
七夜zippoe5 小时前
事务方案选型全景图:金融与电商场景的实战差异与落地指南
java·分布式·事务
杨二K6 小时前
认识HertzBeat的第一天
java·hertzbeat
DevilSeagull6 小时前
JavaScript WebAPI 指南
java·开发语言·javascript·html·ecmascript·html5
期待のcode8 小时前
Spring框架1—Spring的IOC核心技术1
java·后端·spring·架构