java.io.NotSerializableException:java.util.HashMap $ Values

fal*_*lla 8 java serialization esb hashmap map

堆栈跟踪:

org.jboss.remoting.InvocationFailureException: Unable to perform invocation;
nested exception is: java.io.WriteAbortedException: writing aborted;
java.io.NotSerializableException: java.util.HashMap$Values
Run Code Online (Sandbox Code Playgroud)

遗憾的是,日志不显示序列化问题发生的行或类,但调试ESB直到出现问题的步骤所有使用的HashMap都只有Seri​​alizable对象,如String,Long和Date!

此外,调用远程方法时出现问题,该方法为void.

你以前见过这样的东西吗?

fal*_*lla 25

发现问题了!

远程服务试图从以下位置抛出一个封装String Collection的Exception HashMap.values():

if (!identifiersMap.isEmpty()) {
    context.setRollbackOnly();

    BusinessException e = new BusinessException();
    e.setValues(identifiersMap.values()); // here is where the problem is
    throw e;
}
Run Code Online (Sandbox Code Playgroud)

HashMap有一个名为Values的内部类(正如你在这里看到的),它是Collection的一个实现,不是Serializable.因此,抛出具有内容的异常HashMap.values(),远程方法将抛出序列化异常!

例如,ArrayList是Serializable,可用于解决问题.工作代码:

if (!identifiersMap.isEmpty()) {
    context.setRollbackOnly();

    BusinessException e = new BusinessException();
    e.setValues(new ArrayList(apIdentifiersMap.values())); // problem fixed
    throw e;
}
Run Code Online (Sandbox Code Playgroud)

我的情况,远程方法是无效的,它抛出一个异常,但请注意:

如果远程服务返回HashMap $ Values实例,也会发生这种情况,例如:

return hashMap.values(); // would also have serialization problems
Run Code Online (Sandbox Code Playgroud)

再次,解决方案将是:

return new ArrayList(hashMap.values()); // problem solved
Run Code Online (Sandbox Code Playgroud)

  • 我遇到了同样的问题,但是使用keySet,感谢提示! (2认同)