20144

java.io.NotSerializableException is thrown while restarting Vaadin application

When I restart my Tomcat, I get the following error:

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] ...

Answer1:

The warning says that an anonymous inner class (within MyServlet class) must implement serialization. My servlet class contains only one anonymous inner class and it implements Serializable and have a 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) { //... } }); } }

After a little research I found the solution: NotSerializableException on anonymous class So we have to change the anonymous inner class to a static nested 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) { //... } }); }

Or if we don't want multiple objects after deserialization:

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; } }); }

Recommend

  • Using Maven: Getting java.lang.ClassNotFoundException: org.springframework.web.bind.support.WebDataB
  • Deployment of my application in existing tomcat
  • ClassNotFound exception with my JSP and servlet
  • org.apache.struts2.tiles.StrutsTilesListener occasionally not found when starting Tomcat in Eclipse
  • Web Session Replication with HazelCast in Grails - HazelcastSerializationException
  • Serialize Obj with 2 BufferedImage transient fields, second image won't be read
  • Unable to write files in Android P
  • Unable to load class [com.sun.jmx.mbeanserver.RepositorySupport] to check against the @HandlesTypes
  • When to Override writeObject/readObject?
  • Tomcat JDBC MySQL ClassNotFoundException
  • Spring session unable save session to Redis
  • Hazelcast file persistence (MapStore implementation)
  • How can I change config file path in Codeigniter?
  • TypeScript Mapped Types: Get element type of array
  • Unable to import a React Component in my Mocha test
  • Android - What should I use to get data from remote db?
  • How to check when a method is run in another class in Java (method call listener)?
  • scons: How to deal with dynamic targets?
  • Python String Split on pattern without removing delimiter
  • SMT prover yields 'unknown' despite strong proven assertions
  • Getting access to a binary response byte-by-byte in classic asp/JScript
  • AJAX problem in Internet Explorer
  • inserting duplicate records with SQL
  • WPF: Resize item size to have all items visible
  • Firebase firestore cloud functions showing trigger type 'unknown'
  • Updates to SolrConfig.xml file are not being reflected
  • Unmarshalling unknown type code exception while resuming
  • phpmailer - How to verify a sent email arrived at its destination
  • Java: java.util.ConcurrentModificationException
  • Boost binary serialization doesn't work occasionally. The parsed data is corrupted sometimes
  • jhipster run embedded jar with prod profile - issue with liquibase
  • Appium MobileElement swipe returns unknown server error
  • Sybase Error Implicit Conversion from datatype 'VARCHAR' to 'INT' not allowed
  • Building Qt project for C++11 standard
  • Master page gives error
  • Nant, Vault & Windows Integrated Authentication
  • Counter field in MS Access, how to generate?
  • Javascript + PHP Encryption with pidCrypt
  • Websockets service method fails during R startup
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?