Java Kryonet发送对象时出错

时间:2015-10-01 00:24:28

标签: java class object illegalargumentexception kryonet

我正在使用Kryonet进行TCP发送对象,特别是一个名为TransferMessage的对象:

public static class TransferMessage{String text; String username; Color color = Color.black;}

现在,我知道我必须调用kryo.register(TransferMessage.class)但是当我尝试连接到注册了所有类的服务器时,我收到错误:

    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$1.received(ChatServer.java:72)
    at com.esotericsoftware.kryonet.Server$1.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

我不明白,因为虽然我的注册课程使用了Color课程,但我以前从未注册过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();
}

1 个答案:

答案 0 :(得分:1)

默认情况下必须注册

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());
    }
}