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

Stacktrace:

org.jboss.remoting.InvocationFailureException: Unable to perform invocation; nested exception is: java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: java.util.HashMap$Values 

Unfortunately, the log does not show a line or class where the serialization problem occurs, but debugs the ESB until there is a problem with the fact that all used HashMap have only Serializable objects such as String, Long and Date!

In addition, a problem occurs when calling a remote method that is invalid.

Have you seen something like this before?

+7
java hashmap serialization map esb
source share
1 answer

Problem detected!

The remote service tried to throw an Exception by encapsulating the String assembly from 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 called Values (as you can see here) , which is an implementation of Collection and is NOT Serializable. Thus, by creating an exception that has the contents of HashMap.values() , the remote method throws a serialization exception instead!

ArrayList, for example, is Serializable and can be used to solve a problem. Work code:

 if (!identifiersMap.isEmpty()) { context.setRollbackOnly(); BusinessException e = new BusinessException(); e.setValues(new ArrayList(apIdentifiersMap.values())); // problem fixed throw e; } 

In my case, the remote method was invalid and it threw an exception, but note:

This will also happen if the remote service returns an instance of HashMap $ Values, for example:

 return hashMap.values(); // would also have serialization problems 

Once again, the solution will be:

 return new ArrayList(hashMap.values()); // problem solved 
+22
source share

All Articles