【问题标题】:NotSerializableException even while using transientNotSerializableException 即使在使用瞬态时
【发布时间】:2015-11-29 03:30:20
【问题描述】:

我遇到了错误:

警告:无法序列化会话属性 DocumentFieldHelper 会话 {idsession} java.io.NotSerializableException: com.example.DocumentKind
在尝试序列化类时 DocumentFieldHelper。

DocumentFieldHelper 的代码

private class DocumentFieldHelper implements Serializable
        {
            private static final long serialVersionUID = 1L;
            private Map<String, Object> fieldValues;
            private String documentKind;
            public DocumentFieldHelper()
            {
                fieldValues = new HashMap<String, Object>();
            }
            public NativeDockindQuery createQuery()
            {
                try
                {
                    NativeDockindQuery ndq = NativeDockindQuery.create(this.getDocumentKind());
                    return ndq;
                } catch (EdmException e)
                {
                    log.error(e.getMessage(), e);
                }
                return null;
            }
            public String getDocumentKind() {
                return documentKind;
            }

NativeDockindQuery 的代码

public class NativeDockindQuery implements Serializable {
        private static final long serialVersionUID = -2001430456575525419L;
        private transient DocumentKind kind;
        public static NativeDockindQuery create(String kind) throws EdmException {
            return new NativeDockindQuery(DocumentKind.findByCn(kind), false);
        }
        private NativeDockindQuery(DocumentKind kind, boolean checkPermissions) throws EdmException {
            this.kind = kind;
        }
    }

当然,还有更多代码,但我认为这是重要的部分。

我猜 NativeDockindQuery 必须是可序列化的,因为它是 DocumentFieldHelper 方法之一中的返回类型?

我是否有可能因为我使用的是 DocumentKind 的静态方法而遇到这个问题?

【问题讨论】:

  • fieldValues 里面有什么?
  • 我看到两个 documentKind 。一个是 DocumentFieldHelper 中的字符串变量,另一个看起来像 NativeDockindQuery 中的类类型。这样可以吗?我的意思是你在这里弄错了什么?
  • Vlad - fieldValues 对象变量是原语,如 int,long 也是 String。没有什么是无法序列化的(尤其是错误描述中指出的 DocumentKind)
  • SacJn - 不,一切都很好。在 DocumentFieldHelper 中,documentKind 是字符串。在 NativeDockindQuery 中创建时转换为 DocumentKind 类实例。

标签: java serialization transient


【解决方案1】:

好的,我已经做了一些测试,当然,除了我发布的代码之外,其他地方也有问题。我应该提到这段代码不是我的,我只是在看到一些错误时进行更正。 事实证明,DocumentFieldHelper 类是其他类的内部类。外部类声明了 DocumentKind 类的实例变量,这就是问题出现的原因。我刚刚从外部类中创建了 DocumentFieldHelper 独立类,一切正常。 课?序列化内部类时要小心。

感谢 Vlad 和 SacJn 的回复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 2011-11-13
    • 2012-02-10
    • 1970-01-01
    相关资源
    最近更新 更多