Java Kryonet 发送对象时出错
Java Kryonet Error when sending object
我正在使用 Kryonet 进行对象的 TCP 发送,尤其是一个名为 TransferMessage 的对象:
public static class TransferMessage{String text; String username; Color color = Color.black;}
现在,我知道我必须调用 kryo.register(TransferMessage.class)
,但是当我尝试连接到已注册所有 classes 的服务器时,出现错误:
Exception in thread "Server" com.esotericsoftware.kryo.KryoException: java.lang.IllegalArgumentException: Class is not registered: java.awt.Color
Note: To register this class use: kryo.register(java.awt.Color.class);
Serialization trace:
color (com.andrewlalisofficial.MessageTypes$TransferMessage)
at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.write(FieldSerializer.java:585)
at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:213)
at com.esotericsoftware.kryo.Kryo.writeClassAndObject(Kryo.java:571)
at com.esotericsoftware.kryonet.KryoSerialization.write(KryoSerialization.java:50)
at com.esotericsoftware.kryonet.TcpConnection.send(TcpConnection.java:192)
at com.esotericsoftware.kryonet.Connection.sendTCP(Connection.java:59)
at com.esotericsoftware.kryonet.Server.sendToAllTCP(Server.java:435)
at com.andrewlalisofficial.ChatServer.sendMessage(ChatServer.java:204)
at com.andrewlalisofficial.ChatServer.checkCommand(ChatServer.java:124)
at com.andrewlalisofficial.ChatServer.received(ChatServer.java:72)
at com.esotericsoftware.kryonet.Server.received(Server.java:61)
at com.esotericsoftware.kryonet.Connection.notifyReceived(Connection.java:246)
at com.esotericsoftware.kryonet.Server.update(Server.java:208)
at com.esotericsoftware.kryonet.Server.run(Server.java:356)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Class is not registered: java.awt.Color
我不明白,因为虽然我注册的 class 使用颜色 class,但我以前从未注册过 String
对象,所以我看不出有什么区别这是,我该如何解决这个错误?预先感谢您的帮助。
编辑 :
这是在序列化 Color
时显示错误的最小可重现代码:
// this works actually fine
public static void main(String[] args) {
Kryo kryo = new Kryo();
kryo.register(Color.class, new JavaSerializer());
Color color = new Color(15006);
Output output = new Output(new byte[1024]);
kryo.writeObject(output, color);
Input input = new Input(output.getBuffer());
Color color2 = kryo.readObject(input, Color.class);
if (!color.equals(color2)) throw new AssertionError();
}
String
必须默认注册。我自己对这个错误有点惊讶,因为我总是能够序列化 类 我没有注册只是因为 Kryo 拥有默认的序列化器,可以简单地序列化 类。不管怎样,既然 java.awt.Color
实现了 java.io.Serializable
,我就做
kryoRegistrator.register(Color.class, new JavaSerializer());
如果你愿意,你也可以编写自己的序列化程序(使用 Kryo),但我不确定它是否值得(它只是一个简单的序列化整数,标准库没有办法低效地完成它)。如果你想要它,就在这里:
public class ColorSerializer extends Serializer<Color> {
@Override
public Color read(Kryo kryo, Input input, Class<Color> clazz) {
return new Color(input.readInt());
}
@Override
public void write(Kryo kryo, Output output, Color color) {
output.write(color.getRGB());
}
}
我正在使用 Kryonet 进行对象的 TCP 发送,尤其是一个名为 TransferMessage 的对象:
public static class TransferMessage{String text; String username; Color color = Color.black;}
现在,我知道我必须调用 kryo.register(TransferMessage.class)
,但是当我尝试连接到已注册所有 classes 的服务器时,出现错误:
Exception in thread "Server" com.esotericsoftware.kryo.KryoException: java.lang.IllegalArgumentException: Class is not registered: java.awt.Color
Note: To register this class use: kryo.register(java.awt.Color.class);
Serialization trace:
color (com.andrewlalisofficial.MessageTypes$TransferMessage)
at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.write(FieldSerializer.java:585)
at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:213)
at com.esotericsoftware.kryo.Kryo.writeClassAndObject(Kryo.java:571)
at com.esotericsoftware.kryonet.KryoSerialization.write(KryoSerialization.java:50)
at com.esotericsoftware.kryonet.TcpConnection.send(TcpConnection.java:192)
at com.esotericsoftware.kryonet.Connection.sendTCP(Connection.java:59)
at com.esotericsoftware.kryonet.Server.sendToAllTCP(Server.java:435)
at com.andrewlalisofficial.ChatServer.sendMessage(ChatServer.java:204)
at com.andrewlalisofficial.ChatServer.checkCommand(ChatServer.java:124)
at com.andrewlalisofficial.ChatServer.received(ChatServer.java:72)
at com.esotericsoftware.kryonet.Server.received(Server.java:61)
at com.esotericsoftware.kryonet.Connection.notifyReceived(Connection.java:246)
at com.esotericsoftware.kryonet.Server.update(Server.java:208)
at com.esotericsoftware.kryonet.Server.run(Server.java:356)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Class is not registered: java.awt.Color
我不明白,因为虽然我注册的 class 使用颜色 class,但我以前从未注册过 String
对象,所以我看不出有什么区别这是,我该如何解决这个错误?预先感谢您的帮助。
编辑 :
这是在序列化 Color
时显示错误的最小可重现代码:
// this works actually fine
public static void main(String[] args) {
Kryo kryo = new Kryo();
kryo.register(Color.class, new JavaSerializer());
Color color = new Color(15006);
Output output = new Output(new byte[1024]);
kryo.writeObject(output, color);
Input input = new Input(output.getBuffer());
Color color2 = kryo.readObject(input, Color.class);
if (!color.equals(color2)) throw new AssertionError();
}
String
必须默认注册。我自己对这个错误有点惊讶,因为我总是能够序列化 类 我没有注册只是因为 Kryo 拥有默认的序列化器,可以简单地序列化 类。不管怎样,既然 java.awt.Color
实现了 java.io.Serializable
,我就做
kryoRegistrator.register(Color.class, new JavaSerializer());
如果你愿意,你也可以编写自己的序列化程序(使用 Kryo),但我不确定它是否值得(它只是一个简单的序列化整数,标准库没有办法低效地完成它)。如果你想要它,就在这里:
public class ColorSerializer extends Serializer<Color> {
@Override
public Color read(Kryo kryo, Input input, Class<Color> clazz) {
return new Color(input.readInt());
}
@Override
public void write(Kryo kryo, Output output, Color color) {
output.write(color.getRGB());
}
}