需求點(diǎn)
- 實(shí)現(xiàn)Get,Post功能
- 打印Http請(qǐng)求方法,請(qǐng)求地址,Body內(nèi)容
- 返回?cái)?shù)據(jù)到前端。
看代碼如下
HttpServer
public class HttpServer {
public void run(int port){
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup,workGroup) .channel(NIOServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
//設(shè)置http解碼器
ch.pipeline().addLast(new HttpRequestDecoder());
//設(shè)置http內(nèi)容處理器
ch.pipeline().addLast(new HttpObjectAggregator(65536));
//設(shè)置http編碼器
ch.pipeline().addLast(new HttpResponseEncoder());
//自定義服務(wù)處理器
ch.pipeline().addLast(new HttpServerHandler());
}
});
try {
ChannelFuture future = bootstrap.bind(8080).sync();
System.out.println("服務(wù)器啟動(dòng)成功");
future.channel().closeFuture().sync();
}catch (Exception e){
e.printStackTrace();
}finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
public static void main(String[] args) {
new HttpServer().run(8080);
}
}
自定義網(wǎng)絡(luò)I/O時(shí)間處理器HttpServerHandler
public class HttpServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//判斷是不是http請(qǐng)求
if(msg instanceof HttpRequest){
HttpRequest httpRequest = (HttpRequest) msg;
parseUri(httpRequest);
parseHttpMethod(httpRequest);
parseHttpHeaders(httpRequest);
parseBody(httpRequest);
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrAppedBuffer("ok".getBytes()));
HttpUtil.setContentLength(res, res.content().readableBytes());
ctx.writeAndFlush(res);
}
super.channelRead(ctx, msg);
}
/**
* 獲得請(qǐng)求方式
* @param httpRequest
*/
private HttpMethod parseHttpMethod(HttpRequest httpRequest){
HttpMethod httpMethod = httpRequest.method();
System.out.println("method:"+httpMethod.name());
return httpMethod;
}
/**
* 打印頭部信息
* @param httpRequest
*/
private HttpHeaders parseHttpHeaders(HttpRequest httpRequest){
HttpHeaders httpHeaders = httpRequest.headers();
for (Map.Entry<String, String> entry : httpHeaders.entries()) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
return httpHeaders;
}
/**
* 打印請(qǐng)求地址
* @param httpRequest
*/
private void parseUri(HttpRequest httpRequest){
String uri = httpRequest.uri();
System.out.println("uri:"+uri);
}
/**
* 打印請(qǐng)求體
* @param httpRequest
*/
private void parseBody(HttpRequest httpRequest){
if(httpRequest instanceof HttpContent){
HttpContent httpContent = (HttpContent) httpRequest;
System.out.println("content:"+httpContent.content().toString(Charset.defaultCharset()));
}
}
}
啟動(dòng)HttpServer。
網(wǎng)頁(yè)輸入http://localhost:8080/
控制臺(tái)顯示
uri:/
method:GET
Host:localhost:8080
Connection:keep-alive
Cache-Control:max-age=0
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (windows NT 6.1; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/85.0.4183.102 Safari/537.36
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site:noneSec-Fetch-Mode:navigateSec-Fetch-User:?1
Sec-Fetch-Dest:documentAccept-Encoding:gzip, deflate, brAccept-Language:en,zh-CN;q=0.9,zh;q=0.8
content-length:0
content:
PostMan用Post方式請(qǐng)求http://localhost:8080/
控制臺(tái)顯示如下
uri:/
method:POST
Content-Type:text/plain
User-Agent:PostmanRuntime/7.26.3
Accept:*/*
Cache-Control:no-cache
Postman-Token:dbdb420e-5403-40da-8d8f-e2c11029c390
Host:localhost:8080
Accept-Encoding:gzip, deflate, br
Connection:keep-alive
Content-Length:21
content:{
"test":1212
}
代碼實(shí)現(xiàn)起來(lái)不難,我們一步步來(lái)解析,為什么要這樣做。
HttpRequestDecoder
首先HttpRequestDecoder是一個(gè)解碼器,那就是從網(wǎng)絡(luò)讀取字節(jié)流來(lái)處理。
它的主要功能是,解析ByteBuf轉(zhuǎn)化成Http請(qǐng)求的相關(guān)數(shù)據(jù),例如將請(qǐng)求數(shù)據(jù)轉(zhuǎn)化成,HttpRequest/HttpResponse,HttpContent,LastHttpContent。
HttpObjectAggregator
HttpObjectAggregator的作用是聚合,因?yàn)镠ttpRequestDecoder會(huì)解析成多個(gè)Http相關(guān)對(duì)象,它將它們聚合成一個(gè)對(duì)象。
HttpResponseEncoder
HttpResponseEncoder的作用是用來(lái)編碼,將我們響應(yīng)的Http對(duì)象,轉(zhuǎn)化成ByteBuf,進(jìn)行網(wǎng)絡(luò)通信。
我們最終需要把網(wǎng)絡(luò)請(qǐng)求的數(shù)據(jù)轉(zhuǎn)化成http請(qǐng)求的相關(guān)類。
我這里用相關(guān)類來(lái)表示,不特指netty的httpRequest和HttpResponse。因?yàn)槲覀冏约阂部梢宰远x實(shí)現(xiàn)。只要按照http請(qǐng)求的報(bào)文做相應(yīng)的解析即可。
我們來(lái)看下網(wǎng)絡(luò)請(qǐng)求的過(guò)程。

HttpServerHandler是我們自定義的網(wǎng)絡(luò)I/O處理事件。當(dāng)讀數(shù)據(jù)的時(shí)候,判斷msg是HttpRequest類型,說(shuō)明是http請(qǐng)求,做相應(yīng)的處理即可。
小知識(shí)點(diǎn)
對(duì)于一個(gè)陌生的框架,如果想知道它是否有某個(gè)功能,我們直接進(jìn)去看接口有沒(méi)有相關(guān)的實(shí)現(xiàn)即可。
不用百度的,直接看接口,觀察函數(shù)參數(shù),以及返回類型,就可以開始使用。如果調(diào)用不通,看注釋等等,最后才看下百度。
總結(jié)
我們本篇,講解了一個(gè)例子,用netty來(lái)做http服務(wù)器。其中網(wǎng)絡(luò)I/O事件打印了,請(qǐng)求的方式(GET/POST),headers,post body,uri。
有了這幾個(gè)參數(shù),基本上的http相關(guān)操作都可以實(shí)現(xiàn)。
但是HttpRequestDecoder,HttpObjectAggregator,HttpResponseEncoder是如何實(shí)現(xiàn)的呢?