0

我正在使用 GSON。当我启动tomcat时,它给了我错误。我的部分代码:

 @ManagedBean(name = "kpilist")
@ViewScoped
public class KPIListController implements Serializable {

    public List<KPI> getKPI() {
        KPIDAO kpiDAO = new KPIDAO();        
        List<KPI> kpiList = new ArrayList<KPI>();
         Gson gson = new Gson();  
        try {
            kpiList = kpiDAO.getKPI(selectedKPIType);     
            json = gson.toJson(kpiList) ;
            Logger.getLogger(KPIListController.class.getName()).warning("The KPI JSON" + json);
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
        return kpiList;
    }

WARNING: Cannot serialize session attribute com.sun.faces.renderkit.ServerSideStateHelper.LogicalViewMap for session 41AC6C7AB4B0869A29778EA4155BFCA1
    java.io.NotSerializableException: com.google.gson.Gson
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
        at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
        at java.util.HashMap.writeObject(HashMap.java:1014)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:962)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
        at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
        at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1362)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1170)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
        at java.util.HashMap.writeObject(HashMap.java:1014)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:962)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
        at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1362)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1170)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
        at java.util.HashMap.writeObject(HashMap.java:1014)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:962)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
        at java.util.HashMap.writeObject(HashMap.java:1014)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:962)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
        at org.apache.catalina.session.StandardSession.writeObject(StandardSession.java:1692)
        at org.apache.catalina.session.StandardSession.writeObjectData(StandardSession.java:1092)
        at org.apache.catalina.session.StandardManager.doUnload(StandardManager.java:411)
        at org.apache.catalina.session.StandardManager.unload(StandardManager.java:353)
        at org.apache.catalina.session.StandardManager.stopInternal(StandardManager.java:497)
        at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
        at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5449)
        at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
        at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1540)
        at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1530)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
        at java.util.concurrent.FutureTask.run(FutureTask.java:166)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at java.lang.Thread.run(Thread.java:722)

我在使用 GSON 时做错了吗?

4

2 回答 2

2

在您的代码中我看不到它,但看起来有一个成员变量包含 a Gson,可能是间接的(它甚至可以是一个匿名类实例捕获它)。AFAIK,例外说Gson不是Serializable,`这没关系,因为你很可能不想存储它。

于 2012-08-01T10:24:31.673 回答
0

您可以尝试使用“瞬态”关键字:

transient Gson gson = new Gson();

这将在序列化过程中忽略“gson”。您可以在此处阅读更多信息:https ://www.baeldung.com/java-transient-keyword 。

问候。

于 2021-11-05T12:39:48.957 回答