java.io.NotSerializableException is thrown while restarting Vaadin application

痞子三分冷 提交于 2019-12-24 02:23:14

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/26161930/java-io-notserializableexception-is-thrown-while-restarting-vaadin-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!