报错信息
Exception in thread "main" java.lang.IllegalArgumentException: Class is not registered: tech.luxsun.interview.luxinterviewstarter.demo.clone.Rectangle
Note: To register this class use: kryo.register(tech.luxsun.interview.luxinterviewstarter.demo.clone.Rectangle.class);
at com.esotericsoftware.kryo.Kryo.getRegistration(Kryo.java:508)
at com.esotericsoftware.kryo.Kryo.getSerializer(Kryo.java:530)
at com.esotericsoftware.kryo.Kryo.copy(Kryo.java:908)
at tech.luxsun.interview.luxinterviewstarter.demo.clone.Demo.copyByKryo(Demo.java:44)
at tech.luxsun.interview.luxinterviewstarter.demo.clone.Demo.main(Demo.java:37)
原因分析
4.0版本 registrationRequired 的值默认为 false,升级到 5.0 后默认为 true,需要手动设置为 false
解决方案
public static <T> T copyByKryo(T source){
Kryo kryo = new Kryo();
kryo.setRegistrationRequired(false);
return kryo.copy(source);
}