gpt4 book ai didi

java - 即使使用 transient 也不会出现 NotSerializedException

转载 作者:太空宇宙 更新时间:2023-11-04 13:28:58 25 4
gpt4 key购买 nike

我收到错误:

WARNING: Cannot serialize session attribute DocumentFieldHelper for session {idsession} java.io.NotSerializableException: com.example.DocumentKind
while trying to Serialize class 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 的静态方法而遇到此问题?

最佳答案

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

感谢 Vlad 和 SacJn 的回复。

关于java - 即使使用 transient 也不会出现 NotSerializedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32373218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com