6

我正在使用 Javafx,并将我的对象包装到 ListProperty 中,以让 tableview 更新列表对象的任何更改。现在我正在尝试序列化我的项目和对象的 ListProperty,它抛出了这个异常。

java.io.NotSerializableException: javafx.beans.property.SimpleListProperty
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1181)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1541)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1506)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1429)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1175)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at Util.FileManager.serializeProject(FileManager.java:23)
at Controller.FrameworkController.saveProject(FrameworkController.java:549)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:75)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:279)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1435)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.control.MenuItem.fire(MenuItem.java:456)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1197)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer$6.handle(ContextMenuContent.java:1148)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer$6.handle(ContextMenuContent.java:1146)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3328)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
at com.sun.glass.ui.View.notifyMouse(View.java:922)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
at java.lang.Thread.run(Thread.java:724)

我的项目类是这样的,我自己的所有对象都已经实现了可序列化。

public class Project implements Serializable{

private String name;
private String standard;
private ListProperty<Equipment> projectEquipments;

private ListProperty<LegendElement> equipmentsLegend;

public Project() {
    this.projectEquipments = new SimpleListProperty<Equipment>(FXCollections.observableArrayList(new ArrayList<Equipment>()));

    this.equipmentsLegend = new SimpleListProperty<>(FXCollections.observableList(new ArrayList<LegendElement>()));}

我可以做些什么来序列化我的项目和其中的设备列表?

4

6 回答 6

12

这是对我有用的解决方案(在 JPA 实体上序列化 SimpleXXXProperty,其中 xxx 可以是字符串、对象等)

https://gist.github.com/james-d/a7202039b00170256293

你'只是'必须:
1)实现Serializable
2)将所有属性设置为瞬态
3)定义2个这样的特殊方法:

private void writeObject(ObjectOutputStream s) throws IOException {
    s.defaultWriteObject();
    s.writeLong(idProperty().longValue());
    s.writeUTF(aStringProperty().getValueSafe()); // can't be null so use getValueSafe that returns empty string if it's null
    s.writeUTF(anOtherStringPoperty().getValueSafe());
}

private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
    idProperty.set(s.readLong());
    aStringProperty.set(s.readUTF());
    anOtherStringPoperty(s.readUTF());
    // set values in the same order as writeObject()
}

和voilà:)你可以序列化你的对象

MyObject o = new MyObject();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
oos.close();

请注意,这些奇怪的私有方法是由 Java 虚拟机直接调用的,不能像官方文档中提到的那样被 Override、public 或其他。

已编辑:不要对 String 使用 writeChars,而是使用 writeUTF。
请记住,在反序列化过程中,“读取的对象”没有被实例化(没有调用构造函数!),然后所有属性都没有初始化,因此 idProperty.set(o) 将抛出 nullPointerException。
所以你必须创建一个 initMethode 来初始化所有属性。在从 ObjectInputStream 读取数据之前,从您的构造函数和 readObject 方法调用此方法

编辑:我为自动读写做了助手。如果你想使用它,这里是代码:

public class WriteObjectsHelper {

// write a StringProperty to ObjectOutputStream
public static void writeStringProp(ObjectOutputStream s, StringProperty strProp) throws IOException {
    s.writeUTF(strProp.getValueSafe());
}

// write a ListProperty to ObjectOutputStream
public static void writeListProp(ObjectOutputStream s, ListProperty lstProp) throws IOException {
    if(lstProp==null || lstProp.getValue()==null) {
        s.writeInt(0);
        return;
    }
    s.writeInt(lstProp.size());
    for(Object elt:lstProp.getValue()) s.writeObject(elt);
}

// automatic write set of properties to ObjectOutputStream
public static void writeAllProp(ObjectOutputStream s, Property... properties) throws IOException {
    s.defaultWriteObject();
    for(Property prop:properties) {
        if(prop instanceof IntegerProperty) s.writeInt(((IntegerProperty) prop).intValue());
        else if(prop instanceof LongProperty) s.writeLong(((LongProperty) prop).longValue());
        else if(prop instanceof StringProperty) s.writeUTF(((StringProperty)prop).getValueSafe());
        else if(prop instanceof BooleanProperty) s.writeBoolean(((BooleanProperty)prop).get());
        else if(prop instanceof ListProperty) writeListProp(s,(ListProperty)prop);
        else if(prop instanceof ObjectProperty) s.writeObject(((ObjectProperty) prop).get());
        else throw new RuntimeException("Type d'objet incompatible : " + prop.toString());
    }
}
}

public class ReadObjectsHelper {

// Read a ListProperty from ObjectInputStream (and return it)
public static ListProperty readListProp(ObjectInputStream s) throws IOException, ClassNotFoundException {
    ListProperty lst=new SimpleListProperty(FXCollections.observableArrayList());
    int loop=s.readInt();
    for(int i = 0;i<loop;i++) {
        lst.add(s.readObject());
    }

    return lst;
}

// automatic fill a set of properties with values contained in ObjectInputStream
public static void readAllProp(ObjectInputStream s, Property... properties) throws IOException, ClassNotFoundException {
    for(Property prop:properties) {
        if(prop instanceof IntegerProperty) ((IntegerProperty)prop).setValue(s.readInt());
        else if(prop instanceof LongProperty) ((LongProperty)prop).setValue(s.readLong());
        else if(prop instanceof StringProperty) ((StringProperty)prop).setValue(s.readUTF());
        else if(prop instanceof BooleanProperty) ((BooleanProperty)prop).setValue(s.readBoolean());
        else if(prop instanceof ListProperty) ((ListProperty)prop).setValue(readListProp(s));
        else if(prop instanceof ObjectProperty) ((ObjectProperty)prop).setValue(s.readObject());
        else throw new RuntimeException("Unsupported object type : " + prop==null?null:prop.toString());
    }
}
}

他们,这是我的(法语)JPA 实体的一部分:

@Entity
@Table(name="articles")
public class Article implements Serializable, IEntity {

private transient LongProperty idArticle;
public LongProperty idArticleProperty() { return idArticle; }

private transient StringProperty descriptionFr;
public StringProperty descriptionFrProperty() { return descriptionFr; }

private transient StringProperty reference;
public StringProperty referenceProperty() { return reference; }

private transient StringProperty constructeur;
public StringProperty constructeurProperty() { return constructeur; }

private transient StringProperty numSAP;
public StringProperty numSAPProperty() { return numSAP; }

private transient StringProperty descriptionEn;
public StringProperty descriptionEnProperty() { return descriptionEn; }

private transient ListProperty<Article> testList;
public ListProperty<Article> articlesProperty() {return testList; }

public Article() {
    initInstance();
}

/**
 * Need for calling by readObject;
 */
private void initInstance() {
    idArticle=new SimpleLongProperty();
    descriptionFr=new SimpleStringProperty();
    descriptionEn=new SimpleStringProperty();
    reference=new SimpleStringProperty();
    constructeur=new SimpleStringProperty();
    numSAP=new SimpleStringProperty();
    testList=new SimpleListProperty<>(FXCollections.observableArrayList());
}

private void writeObject(ObjectOutputStream s) throws IOException {
    WriteObjectsHelper.writeAllProp(s,idArticle,descriptionFr,reference,constructeur,numSAP,descriptionEn,testList);
}

private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
    initInstance();
    ReadObjectsHelper.readAllProp(s, idArticle,descriptionFr,reference,constructeur,numSAP,descriptionEn,testList);
}

@Column(name = "idArticle")
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getIdArticle() {
    return idArticle.get();
}

public void setIdArticle(Long idArticle) {
    this.idArticle.set(idArticle);
}
//...
}
于 2017-08-12T21:26:33.197 回答
4

您将需要为您的项目创建自定义序列化。

有关详细信息,请参阅以下文章:

  1. 关于 Java 中的自定义序列化的 StackOverflow 问题
  2. 关于 Java 中自定义序列化的官方 Oracle 教程

或者,您可以使用JAXBjavax.json等技术使用 JSON 或 XML 等文本格式编写序列化。

所有 JavaFX 项目(以及从 JavaFX 2.x 开始的 JavaFX 框架中的几乎所有对象)都没有实现 Serializable,因此您不能直接序列化 JavaFX 属性。

于 2013-09-13T17:41:05.123 回答
1

为避免 NotSerializableException,请确保:

  1. 你的类实现了 Serializable
  2. 所有非原始成员都实现 Serializable (或者是瞬态的)
  3. 如果您的类是内部类,它要么是静态的,要么外部类实现 Serializable

除此之外,您还需要为每个 Serializable 类定义serialVersionUID。检查以上所有 3 种情况加上:

  1. 所有可序列化的超类
  2. 如果您的班级是匿名班级,请也在那里定义它

注意:您的代码有时可能在没有 serialVersionUID 的情况下运行,但请阅读 Serializable 的 javadoc 中的最后一段以了解它为什么会这样

取决于环境是一个问题。


有一个 VM 选项可以向异常添加详细信息。它将显示无法序列化的根类和嵌套类,并帮助您找出缺少的内容:

-Dsun.io.serialization.extendedDebugInfo=true
于 2014-08-19T03:02:18.117 回答
0

我有同样的问题:我的解决方案。

正在使用接口 Externalizable 并编写我自己的 writeExternal 和 readExternal 方法。

后来我使用 XStream 时遇到了 Externalizable 的问题。快速修复是使用下面的代码更改 ExternalizableConverter 的优先级。

    xstream.registerConverter(new ReflectionConverter(xstream.getMapper(),
            xstream.getReflectionProvider()) {
        public boolean canConvert(Class type) {
            return Externalizable.class.isAssignableFrom(type);
        }
    }, XStream.PRIORITY_LOW);
于 2014-08-15T19:05:09.610 回答
0

尝试创建一个演示类作为前端和后端之间的桥梁。使用可序列化对主类文件执行后端操作,但在创建的演示类的帮助下访问它,数据类型为 SimplexxxProperty。在演示类的构造函数中执行转换操作或尝试从类中删除 tostring()。

于 2019-04-20T18:10:26.997 回答
0

该属性Object需要实现Serializable接口。下面是SimpleObjectProperty, 对于通用对象的一个​​简单扩展,它是可序列化的:

public class WriteableObjectProperty<T> extends SimpleObjectProperty<T> implements Serializable {

    public WriteableObjectProperty () {
        super();
    }

    public WriteableObjectProperty (T obj) {
        super(obj);
    }


    private void writeObject (ObjectOutputStream s) throws IOException {
        s.defaultWriteObject();
        s.writeObject(get());
    }

    private void readObject (ObjectInputStream s) throws IOException, ClassNotFoundException {
        s.defaultReadObject();
        set((T) s.readObject());
    }
}
于 2020-02-24T15:50:41.123 回答