2011年12月27日星期二

Could not deserialize session data java.io.NotSerializableException

<2011-12-20 下午08时51分00秒 GMT+08:00> <Error> <HTTP Session> <BEA-100028> <Could not deserialize session data.
java.io.NotSerializableException: com.capinfo.service.impl.tcs.UnitUsersServiceImpl$1
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
        at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
        at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:422)
        at java.util.TreeMap.writeObject(TreeMap.java:2243)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        Truncated. see log file for complete stacktrace
UnitUsersServiceImpl$1是
TreeMap<Long,TcsFunc> topTcsFuncList = new TreeMap<Long,TcsFunc>(new Comparator()  {
 public int compare(Object o1, Object o2) {
     long l2 = Long.valueOf(String.valueOf(o2));
     long l1 = Long.valueOf(String.valueOf(o1));
     if(l2>l1)
  return -1;
     else if(l2<l1)
  return 1;
     else
  return 0;
      //return String.valueOf(o2).compareTo(String.valueOf(o1));
 }
 });

参考
http://www.tek-tips.com/viewthread.cfm?qid=1042413
Make sure the object you are putting into the session is serializable and also
the non-transient object it aggregates are also serializable.
If any of the non-transient objects in the entire object
graph are not serializable, you will receive this error message.


http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4330877

In order for classes such java.util.Treemap and java.util.TreeSet to be
serialized the Comparators(java.util.Comparator) also need to be serializable.

改成
TreeMap<Long,TcsFunc> topTcsFuncList = new TreeMap<Long,TcsFunc>(new MyComparator());

----
/**
* 因为用到这个比较器的TreeMap需要放到Session中
* 所以这个比较器也需要实现Serializable
*/
class MyComparator implements Comparator,Serializable{
 public int compare(Object o1, Object o2) {
     long l2 = Long.valueOf(String.valueOf(o2));
     long l1 = Long.valueOf(String.valueOf(o1));
     if(l2>l1)
  return -1;
     else if(l2<l1)
  return 1;
     else
  return 0;
     //return String.valueOf(o2).compareTo(String.valueOf(o1));
 }
}

没有评论:

发表评论