2016-07-27 58 views
1

我正在寫一個JavaEE應用,我有以下問題:如果我在類(TraceMessage)中的一個初始化任何新的變量並啓動應用程序,我收到以下錯誤:的JavaEE - InvalidClassException

java.io.InvalidClassException: com.nsn.extmon.util.TraceMessage; local class incompatible: stream classdesc serialVersionUID = -5424907704652912574, local class serialVersionUID = -5748024201087063218 
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:612) 
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1630) 
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1521) 
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1779) 
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353) 
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373) 
at java.util.ArrayList.readObject(ArrayList.java:771) 
at sun.reflect.GeneratedMethodAccessor355.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1058) 
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1907) 
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1806) 
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353) 
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373) 
at com.nsn.extmon.databaseHandling.ResultDataHandler.getTraceMessages(ResultDataHandler.java:137) 
at com.nsn.extmon.util.TraceResultBean.traceRead(TraceResultBean.java:50) 
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:606) 
at org.apache.el.parser.AstValue.invoke(AstValue.java:278) 
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274) 
at org.apache.myfaces.view.facelets.el.ContextAwareTagMethodExpression.invoke(ContextAwareTagMethodExpression.java:96) 
at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72) 
at javax.faces.component.UICommand.broadcast(UICommand.java:120) 
at org.richfaces.component.RowKeyContextEventWrapper.broadcast(RowKeyContextEventWrapper.java:104) 
at org.richfaces.component.UIDataAdaptor.broadcast(UIDataAdaptor.java:452) 
at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:1013) 
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:284) 
at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1302) 
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:745) 
at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:38) 
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:170) 
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117) 
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:197) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
at com.nsn.extmon.authentication.LoginFilter.doFilter(LoginFilter.java:33) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:611) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409) 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044) 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607) 
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1721) 
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1679) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
at java.lang.Thread.run(Thread.java:745) 

我不知道這個錯誤是什麼,我只知道如果我修改變量名稱,那麼一切工作正常。只有當我聲明新變量時纔會出現該錯誤。

感謝您的建議。發生在當你deserialise這種情況

+0

看一些代碼示例將有助於理解這個 –

回答

4

一種InvalidClassException類異常(接收,加載等)一個對象到Java過程具有不同的類版本序列化(發送期間使用的一個,節約等)。

看起來您正在將對象保存到數據庫中(com.nsn.extmon.databaseHandling.ResultDataHandler?)。我認爲數據庫中存在序列化的斑點,並使用不同版本的類(「添加字段」)進行序列化,然後再次從數據庫中讀取它們。

如果要添加字段,你應該保持serialVersionUID相同(合同破變化)

對於實例

public class TraceMessage implements Serializable { 

    private static final long serialVersionUID = 1L; 
    … 
} 

如果是合同破更改,那麼您必須確保兩端具有相同的類版本。

如果您要在應用程序之間發送消息,那麼您可能還需要查看一個較脆弱的協議,例如google protobuf。

EDIT(見註釋):如果要加載存儲在數據庫中的新類的老班,然後設定serialVersionUID-5424907704652912574

+0

這只是提交併從數據庫中讀取數據,我嘗試添加'private static final long serialVersionUID = 1L;'但後來我得到以下錯誤:'java.io.InvalidClassException:com.nsn.extmon.util.TraceMessage;本地類不兼容:stream classdesc serialVersionUID = -5424907704652912574,本地類serialVersionUID = 1'。我知道已經序列化的對象ID保持不變。 –

+0

您需要做一些遷移工作:您可能仍然在數據庫中使用舊版本的類編寫對象。您必須使用舊的類文件加載它們,或將「serialVersionUID」設置爲「-5424907704652912574」。 –

+0

我將ID更改爲-5424907704652912574,它現在正在工作。非常感謝你。 :) –