java.io.NotSerializableException: java.util.HashMap$Values
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18700751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.io.NotSerializableException: java.util.HashMap$Values
提问by falsarella
Stacktrace:
堆栈跟踪:
org.jboss.remoting.InvocationFailureException: Unable to perform invocation;
nested exception is: java.io.WriteAbortedException: writing aborted;
java.io.NotSerializableException: java.util.HashMap$Values
Sadly, the log doesn't show the line or the class where the serialization problem occurs, but debugging the ESB until the step where problem occurs all the HashMap's that are used have only Serializable objects like String, Long and Date!
遗憾的是,日志没有显示出现序列化问题的行或类,但是在出现问题的步骤之前调试 ESB,所有使用的 HashMap 都只有可序列化的对象,如 String、Long 和 Date!
Also, the problem occurs when calling a remote method, which is void.
此外,在调用远程方法时会出现此问题,该方法无效。
Have you seen something like this before?
你以前见过这样的吗?
采纳答案by falsarella
Found the problem!
发现问题了!
The remote service was trying to throw an Exception encapsulating a Collection of String from HashMap.values()
:
远程服务试图抛出一个异常,封装了一个来自HashMap.values()
以下的字符串集合:
if (!identifiersMap.isEmpty()) {
context.setRollbackOnly();
BusinessException e = new BusinessException();
e.setValues(identifiersMap.values()); // here is where the problem is
throw e;
}
HashMap has an inner class named Values(as you can see here), which is an implementation of Collection and is NOT Serializable. So, throwing an exception having the content of HashMap.values()
, the remote method will throw a serialization exception instead!
HashMap 有一个名为Values的内部类(如您所见),它是 Collection 的实现,不可序列化。因此,抛出内容为 的异常HashMap.values()
,远程方法将抛出序列化异常!
ArrayList, for example, is Serializable and could be used to resolve the issue. Working code:
例如,ArrayList 是可序列化的,可用于解决该问题。工作代码:
if (!identifiersMap.isEmpty()) {
context.setRollbackOnly();
BusinessException e = new BusinessException();
e.setValues(new ArrayList(apIdentifiersMap.values())); // problem fixed
throw e;
}
My case, the remote method was void and it was throwing an Exception, but note:
我的情况,远程方法是无效的,它抛出一个异常,但请注意:
It will also occur if the remote service return a HashMap$Values instance, for example:
如果远程服务返回一个 HashMap$Values 实例,也会发生这种情况,例如:
return hashMap.values(); // would also have serialization problems
Once again, the solution would be:
再次,解决方案是:
return new ArrayList(hashMap.values()); // problem solved