为什么 writeObject 会抛出 java.io.NotSerializableException,我该如何修复它



我有这个例外,我不明白为什么会抛出它,或者我应该如何处理它。

try {
    os.writeObject(element);
} catch (IOException e) {
    e.printStackTrace();
}

其中element是一个包含其他TransformGroups类 Atom 实例的TransformGroup

public class Atom extends Group implements Serializable{
    float pozX,pozY;
    Group group= new Group();   
    Color3f blue = new Color3f(new Color(255));
    Color3f black = new Color3f(new Color(0));
    Sphere AtSph=new Sphere();
    public Atom(final float WEIGHT, final int BOUNDS,final float radius,Color3f color)
    {
        AppSetting ap= new AppSetting(color, black);
        AtSph=new Sphere(radius,1,100,ap);
    }
}

完整的错误日志:

java.io.NotSerializableException: javax.media.j3d.TransformGroup
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at cls.MolecularBuilder.addAtom(MolecularBuilder.java:511)
    at cls.MolecularBuilder$Console.HidrogenItemActionPerformed(MolecularBuilder.java:897)
    at cls.MolecularBuilder$Console$2.actionPerformed(MolecularBuilder.java:746)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

AppSetting(在 Atom 类中)只是一个扩展外观的自定义类。

对象的字段依次具有它们的字段,其中一些字段不实现Serializable 。在您的情况下,违规类是TransformGroup .如何解决?

  • 如果课程是你的,请将其Serializable
  • 如果类是第三方类,但您不需要序列化形式的类,请将该字段标记为transient
  • 如果您需要其数据和第三方数据,请考虑其他序列化方法,例如 JSON、XML、BSON、MessagePack 等,您可以在其中序列化第三方对象而无需修改其定义。
序列化

内部类实例时可能会发生java.io.NotSerializableException,因为:

序列化此类内部类实例将导致序列化以及其关联的外部类实例

内部类(即,不是静态成员类),包括本地类和匿名类,是强烈劝阻

参考: 可序列化接口

通过实现接口java.io.Serializable使类可序列化。

  • java.io.Serializable - 标记接口,其中没有任何方法。
  • 标记接口的用途 - 告诉ObjectOutputStream此对象是可序列化的对象。

如上所述,如果该类是第三方类,则需要将其转换为 JSON/XML/BSON 对象,因为第三方类对象无法序列化。

正在处理我的项目并遇到了同样的问题,我使用 gson 库将我的类对象转换为 JSON 字符串。之后,我在该对象中使用了这个字符串,并通过 ObjectOutputStream 对象传递了它。在客户端,我使用相同的方法将 JSON 字符串恢复为第 3 方类对象。

你可以从这里下载罐子:https://jar-download.com/artifacts/com.google.code.gson/gson/2.8.2/source-code!

这是我是如何做到的:

// Method that returns String through a Object as a parameter i.e to be converted
 public static String  generateJSONStringFromObject(ThirdPartyClass object) throws JsonProcessingException{
            ObjectMapper mapper = new ObjectMapper();
            mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
            String jsonString = mapper.writeValueAsString(object);
            
                return jsonString;
        }
// Method that returns Object through a String as a parameter 
 public static ThirdPartyClass generateObjectFromJSONString(String jsonString){
            Gson gson = new Gson();
            ThirdPartyClass thirdPartyClassObject = gson.fromJson(jsonString, ThirdPartyClass.class);
            return thirdPartyClassObject;
        }

相关内容

  • 没有找到相关文章

最新更新