io.netty.handler.codec.serialization.ObjectDecoder 将ByteBuf[]反序列化为java对象。 A decoder which deserializes the received ByteBufs into Java objects. io.netty.handler.codec.serialization.ObjectEncoder 编码器,将java对象序列化为一个netty的ByteBuf对象。 An encoder which serializes a Java object into a ByteBuf. 要求传送的类实现java.io.serializable接口。用法见下。
Bootstrap b = new Bootstrap(); b.handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(null)), new TcpClientHandler()); } });完整的项目示例见github。
https://github.com/chuchu9293/NettyTcpObjOrdering