当我重新启动Tomcat时,出现以下错误:
2014;10;01; 15;49;47;055; WARN; com.vaadin.event.ListenerMethod;[localhost-startStop-2]; Error in serialization of the application: Class com.aaa.bbb.MyServlet$1 must implement serialization.
2014;10;01; 15;49;47;055; WARN; org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/my-vaadin];[localhost-startStop-2]; Cannot serialize session attribute com.vaadin.server.VaadinSession.MyServlet for session ...
java.io.NotSerializableException: org.apache.catalina.loader.WebappClassLoader
at java.io.ObjectOutputStream.writeObject0(Unknown Source) ~[na:1.6.0_31]
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) ~[na:1.6.0_31]
at java.io.ObjectOutputStream.writeSerialData(Unknown Source) ~[na:1.6.0_31]
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) ~[na:1.6.0_31]
at java.io.ObjectOutputStream.writeObject0(Unknown Source) ~[na:1.6.0_31]
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) ~[na:1.6.0_31]
at java.io.ObjectOutputStream.writeSerialData(Unknown Source) ~[na:1.6.0_31]
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) ~[na:1.6.0_31]
at java.io.ObjectOutputStream.writeObject0(Unknown Source) ~[na:1.6.0_31]
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) ~[na:1.6.0_31]
at java.io.ObjectOutputStream.writeSerialData(Unknown Source) ~[na:1.6.0_31]
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) ~[na:1.6.0_31]
at java.io.ObjectOutputStream.writeObject0(Unknown Source) ~[na:1.6.0_31]
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) ~[na:1.6.0_31]
at java.io.ObjectOutputStream.defaultWriteObject(Unknown Source) ~[na:1.6.0_31]
at com.vaadin.event.ListenerMethod.writeObject(ListenerMethod.java:88) ~[vaadin-server-7.2.7.jar:7.2.7]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_31]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.6.0_31]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.6.0_31]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.6.0_31]
...
答案 0 :(得分:3)
警告说匿名内部类(在MyServlet
类中)必须实现序列化。我的servlet类只包含一个匿名内部类,它实现了Serializable
并且有一个serialVersionUID
:
public class MyServlet extends VaadinServlet implements SessionInitListener {
//...
@Override
public void sessionInit(SessionInitEvent event) throws ServiceException {
event.getSession().addBootstrapListener(new BootstrapListener() {
private static final long serialVersionUID = 1L;
@Override
public void modifyBootstrapPage(BootstrapPageResponse response) {
//...
}
@Override
public void modifyBootstrapFragment(BootstrapFragmentResponse response) {
//...
}
});
}
}
经过一番研究后,我找到了解决方案:NotSerializableException on anonymous class
所以我们必须将匿名内部类更改为静态嵌套类:
public class MyServlet extends VaadinServlet implements SessionInitListener {
//...
@Override
public void sessionInit(SessionInitEvent event) throws ServiceException {
event.getSession().addBootstrapListener(new MyBootstrapListener());
}
private static class MyBootstrapListener implements BootstrapListener {
private static final long serialVersionUID = 1L;
@Override
public void modifyBootstrapPage(BootstrapPageResponse response) {
//...
}
@Override
public void modifyBootstrapFragment(BootstrapFragmentResponse response) {
//...
}
});
}
或者如果我们在反序列化后不想要多个对象:
public class MyServlet extends VaadinServlet implements SessionInitListener {
//...
@Override
public void sessionInit(SessionInitEvent event) throws ServiceException {
event.getSession().addBootstrapListener(MyBootstrapListener.INSTANCE);
}
private static class MyBootstrapListener implements BootstrapListener {
private static final long serialVersionUID = 1L;
private static final MyBootstrapListener INSTANCE = new MyBootstrapListener();
@Override
public void modifyBootstrapPage(BootstrapPageResponse response) {
//...
}
@Override
public void modifyBootstrapFragment(BootstrapFragmentResponse response) {
//...
}
private Object readResolve() {
return INSTANCE;
}
});
}