gpt4 book ai didi

java - Netty 如何发送对象

转载 作者:行者123 更新时间:2023-12-01 13:13:35 25 4
gpt4 key购买 nike

如何使用netty发送对象。当我尝试发送 String 对象时,一切正常。

channel.writeAndFlush(new String("df"));

但是当我尝试发送一个简单的对象服务器时出现错误

public class Message implements Serializable {

private static final long serialVersionUID = 1L;
}
channel.writeAndFlush(new Message());
<小时/>
io.netty.channel.DefaultChannelPipeline$TailHandler exceptionCaught
WARNING: An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
io.netty.handler.codec.DecoderException: java.lang.NullPointerException
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:259)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:141)
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:340)
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:326)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:116)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:494)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:461)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:378)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:350)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
at io.netty.handler.codec.serialization.CompactObjectInputStream.readClassDescriptor(CompactObjectInputStream.java:55)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1601)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at io.netty.handler.codec.serialization.ObjectDecoder.decode(ObjectDecoder.java:73)
at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:343)
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:228)
... 11 more
<小时/>
class ChatServerInitializer extends ChannelInitializer<SocketChannel> {

@Override
protected void initChannel(SocketChannel arg0) throws Exception
{
ChannelPipeline pipeline = arg0.pipeline();
pipeline.addLast("decoder", (new ObjectDecoder(null)));
pipeline.addLast("encoder", new ObjectEncoder());
pipeline.addLast("handler", new ChatServerHandler());
}
}

最佳答案

您在构造 ObjectDecoder 时指定了 null,这导致此处出现 NullPointerException。相反,您必须指定类似 ClassResolvers.weakCachingResolver(MyMessage.class.getClassLoader()) 的内容。

关于java - Netty 如何发送对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22643070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com