我正在使用我的代码遇到此异常,我不太确定如何修复它。我正在使用Apache Tomcat v6.0。我认为类“TransactionStorage”需要实现Serializable,但我该如何去做呢?
"WARNING: Cannot serialize session attribute sortedList for session 4A5378C83A44BC037B0F1985EAC8DD31
java.io.NotSerializableException: com.middleware.hts.TransactionStorage
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1081)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:302)
at java.util.ArrayList.writeObject(ArrayList.java:569)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:592)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:917)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1339)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1290)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1079)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:302)
at org.apache.catalina.session.StandardSession.writeObject(StandardSession.java:1572)
at org.apache.catalina.session.StandardSession.writeObjectData(StandardSession.java:1015)
at org.apache.catalina.session.StandardManager.doUnload(StandardManager.java:528)
at org.apache.catalina.session.StandardManager.unload(StandardManager.java:469)
at org.apache.catalina.session.StandardManager.stop(StandardManager.java:678)
at org.apache.catalina.core.StandardContext.stop(StandardContext.java:4882)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3454)
at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1361)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1653)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1662)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1662)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1642)
at java.lang.Thread.run(Thread.java:595)
TransactionStorage类目前如下所示:
public class TransactionStorage implements Comparable<TransactionStorage>{
private List<HubTransaction> success = new ArrayList<HubTransaction>();
private List<HubTransaction> failure = new ArrayList<HubTransaction>();
private List<HubTransaction> undetermine = new ArrayList<HubTransaction>();
private String process;
private String client;
public TransactionStorage(String process, String client){
this.process = new String(process);
this.client = new String(client);
}
public String getProcess(){
return this.process;
}
public String getClient(){
return this.client;
}
public List<HubTransaction> getSuccess(){
return this.success;
}
public List<HubTransaction> getFailure(){
return this.failure;
}
public List<HubTransaction> getUndetermine(){
return this.undetermine;
}
public void addSuccess(HubTransaction ht){
this.success.add(ht);
}
public void addFailure(HubTransaction ht){
this.failure.add(ht);
}
public void addUndetermine(HubTransaction ht){
this.undetermine.add(ht);
}
public int totalTrans(){
return this.success.size() + this.failure.size() + this.undetermine.size();
}
public int compareTo(TransactionStorage arg) {
double thisRatio = (double)this.success.size() / (double)this.totalTrans();
double argRatio = (double)arg.getSuccess().size() / (double)arg.totalTrans();
if(thisRatio < argRatio){
return 1;
}else if(thisRatio > argRatio){
return -1;
}else{
return 0;
}
}
}
答案 0 :(得分:3)
java.io.NotSerializableException: com.middleware.hts.TransactionStorage
您需要更改
public class TransactionStorage implements Comparable<TransactionStorage>{
到
public class TransactionStorage implements Comparable<TransactionStorage>, Serializable {
其中Serializable
为java.io.Serializable
。
答案 1 :(得分:2)
除非您有权更改该课程,否则看起来您无法做到。如果你这样做,那很好。如果不这样做,则必须编写一个适当覆盖以下方法的包装类。
private void writeObject(java.io.ObjectOutputStream out) throws IOException
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;