unsupported message type: DefaultFullHttpResponse (expected: ByteBuf, FileRegion) 原因以及解决办法

本文详细解析了使用Netty构建HTTP服务器时,因HttpObjectAggregator顺序不当导致的连接问题。通过调整HttpObjectAggregator在管道中的位置,解决了Android客户端链接时出现的错误。文章提供了正确的代码示例,确保服务器能正确处理HTTP请求。
部署运行你感兴趣的模型镜像

使用netty做http服务器的时候 用android链接 会出现这个错误

 

原因是http-aggregator顺序有问题

(ps:目前大部分国内博客都是这个排序有点坑爹):

官方文档说明:For convenience, consider putting a HttpServerCodec before the HttpObjectAggregator as it functions as both a HttpRequestDecoder and a HttpResponseEncoder.

package com.sencorsta.ids.httptest;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * Created by apple on 17/10/21.
 */
public class HttpServer {

	public static void start(final int port) throws Exception {
		EventLoopGroup boss = new NioEventLoopGroup();
		EventLoopGroup woker = new NioEventLoopGroup();
		ServerBootstrap serverBootstrap = new ServerBootstrap();
		HttpServerHandler httpServerHandler=new HttpServerHandler();
		try {

			serverBootstrap.channel(NioServerSocketChannel.class).group(boss, woker)
					.childOption(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.SO_BACKLOG, 1024)
					.childHandler(new ChannelInitializer<SocketChannel>() {
						@Override
						protected void initChannel(SocketChannel ch) throws Exception {
							ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());      
                            ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));//这里的位置有问题
							ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
							ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
							ch.pipeline().addLast(httpServerHandler);
						}
					});

			ChannelFuture future = serverBootstrap.bind(port).sync();
			future.channel().closeFuture().sync();
		} finally {
			boss.shutdownGracefully();
			woker.shutdownGracefully();
		}
	}

	public static void main(String[] args) throws Exception {
		start(8080);
	}
}

把上面的ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));这一行

放到coder下面就可以了

package com.sencorsta.ids.httptest;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * Created by apple on 17/10/21.
 */
public class HttpServer {

	public static void start(final int port) throws Exception {
		EventLoopGroup boss = new NioEventLoopGroup();
		EventLoopGroup woker = new NioEventLoopGroup();
		ServerBootstrap serverBootstrap = new ServerBootstrap();
		HttpServerHandler httpServerHandler=new HttpServerHandler();
		try {

			serverBootstrap.channel(NioServerSocketChannel.class).group(boss, woker)
					.childOption(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.SO_BACKLOG, 1024)
					.childHandler(new ChannelInitializer<SocketChannel>() {
						@Override
						protected void initChannel(SocketChannel ch) throws Exception {
							ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
							ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
							ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));
							ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
							ch.pipeline().addLast(httpServerHandler);
						}
					});

			ChannelFuture future = serverBootstrap.bind(port).sync();
			future.channel().closeFuture().sync();
		} finally {
			boss.shutdownGracefully();
			woker.shutdownGracefully();
		}
	}

	public static void main(String[] args) throws Exception {
		start(8080);
	}
}

参考资料:官方说明

您可能感兴趣的与本文相关的镜像

Seed-Coder-8B-Base

Seed-Coder-8B-Base

文本生成
Seed-Coder

Seed-Coder是一个功能强大、透明、参数高效的 8B 级开源代码模型系列,包括基础变体、指导变体和推理变体,由字节团队开源

[main] INFO client.TcpClient - 客户端连接成功,目标地址:127.0.0.1:8080 [main] INFO client.TcpClient - 客户端已关闭 Exception in thread "main" java.lang.UnsupportedOperationException: unsupported message type: GreetRequest (expected: ByteBuf, FileRegion) at io.netty.channel.nio.AbstractNioByteChannel.filterOutboundMessage(AbstractNioByteChannel.java:287) at io.netty.channel.AbstractChannel$AbstractUnsafe.write(AbstractChannel.java:739) at io.netty.channel.DefaultChannelPipeline$HeadContext.write(DefaultChannelPipeline.java:1386) at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:823) at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:752) at io.netty.handler.codec.MessageToByteEncoder.write(MessageToByteEncoder.java:120) at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:827) at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:752) at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:102) at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:827) at io.netty.channel.AbstractChannelHandlerContext$WriteTask.run(AbstractChannelHandlerContext.java:1136) at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:148) at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:141) at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:507) at io.netty.channel.SingleThreadIoEventLoop.run(SingleThreadIoEventLoop.java:183) at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:1073) at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.lang.Thread.run(Thread.java:750) Suppressed: java.util.concurrent.CompletionException: Rethrowing promise failure cause at io.netty.util.concurrent.DefaultPromise.rethrowIfFailed(DefaultPromise.java:685) at io.netty.util.concurrent.DefaultPromise.sync(DefaultPromise.java:419) at io.netty.channel.DefaultChannelPromise.sync(DefaultChannelPromise.java:119) at io.netty.channel.DefaultChannelPromise.sync(DefaultChannelPromise.java:30) at client.TcpClient.sendRequest(TcpClient.java:130) at client.TcpClient.main(TcpClient.java:173)
08-23
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值