Java.io.InvalidClassException: ; incompatible types for field exception tracking

Source: Internet
Author: User
Tags serialization
java.io.InvalidClassException: <className>; incompatible types for field <fieldName> exception tracking Exception Information

The following exception information was generated in the background log:

Java.io.InvalidClassException: <className>; Incompatible types for field <fieldName> at Java.io.ObjectStreamClass.matchFields (objectstreamclass.java:229 9) ~[?:1.8.0_74] at Java.io.ObjectStreamClass.getReflector (objectstreamclass.java:2193) ~[?:1.8.0_74] at J Ava.io.ObjectStreamClass.initNonProxy (objectstreamclass.java:669) ~[?:1.8.0_74] at Java.io.ObjectInputStream.read Nonproxydesc (objectinputstream.java:1623) ~[?:1.8.0_74] at Java.io.ObjectInputStream.readClassDesc (objectinputstr eam.java:1518) ~[?:1.8.0_74] at Java.io.ObjectInputStream.readOrdinaryObject (objectinputstream.java:1774) ~[?:1.8. 0_74] at Java.io.ObjectInputStream.readObject0 (objectinputstream.java:1351) ~[?:1.8.0_74] at Java.io.Objec Tinputstream.readarray (objectinputstream.java:1707) ~[?:1.8.0_74] at Java.io.ObjectInputStream.readObject0 (Object inputstream.java:1345) ~[?:1.8.0_74] at Java.io.ObjectInputStream.defaultReadFields (objectinputstream.java:2000) ~[?:1.8.0_74] at Java.io.ObjectInputStream.readSerialData (objectinputstream.java:1924) ~[?:1 .8.0_74] at Java.io.ObjectInputStream.readOrdinaryObject (objectinputstream.java:1801) ~[?:1.8.0_74] at JAV A.io.objectinputstream.readobject0 (objectinputstream.java:1351) ~[?:1.8.0_74] at Java.io.ObjectInputStream.readOb Ject (objectinputstream.java:371) ~[?:1.8.0_74]

The

involves outputinputstream.readobject, so guessing is a problem that arises when an object is deserialized. Exception Source

 private static objectstreamfield[] Matchfields (objectstreamfield[] fields, ObjectStreamClass Localdesc) throws invalidclassexception{//... for (int j = 0; J < Localfi Elds.length;
                J + +) {Objectstreamfield lf = localfields[j];
                        if (F.getname (). Equals (Lf.getname ()) {if (f.isprimitive () | | lf.isprimitive ()) && F.gettypecode ()!= Lf.gettypecode ()) {throw new Invalidclassex
                    Ception (Localdesc.name, "incompatible types for field" + F.getname ());
                            } if (Lf.getfield ()!= null) {m = new Objectstreamfield (
                    Lf.getfield (), lf.isunshared (), false); else {m = new Objectstreamfield (Lf.getname (), Lf.getsigNature (), lf.isunshared ()); }
                }
            }
     // ...
 }
Abnormal Recurrence
Import java.io.Serializable;

public class Request implements Serializable {

    private static final long Serialversionuid = 3663364724485038109l;
  
   public Integer status;

    Public Integer AddressStatus;

    Setter Getter
}
  

Serializing a Request object

public class Serialization {public static void main (string[] args) throws IOException, classnotfoundexception{
        Request R1 = new request ();
        R1.setaddressstatus (100);
        R1.setstatus (200);
        Request R2 = new request ();
        R2.setaddressstatus (300);

        R2.setstatus (400);

        File File = new file ("D:/mysql/workspace/spring/resources/request.ser");

        Serializable (file, R1, r2);
        Request[] rs = deserializable (file);
    for (Request request:rs) System.out.println (request.getaddressstatus () + "" + request.getstatus ()); public static void serializable (file file, Request ... requests) throws IOException {ObjectOutputStream str
        EAM = new ObjectOutputStream (new FileOutputStream (file));
        if (requests!= null) Stream.writeobject (requests);
    Stream.Close (); public static request[] deserializable (file file) throws IOException, ClassNotFoundException {objectiNputstream stream = new ObjectInputStream (new FileInputStream (file));
        Request[] Requests = (request[]) stream.readobject ();
        Stream.Close ();
    return requests; }
}

Modify Request Scope type

Import java.io.Serializable;

public class Request implements Serializable {

    private static final long Serialversionuid = 3663364724485038109l;
  
   public int status;

    public int addressstatus;

    Setter Getter
}
  

Deserializing objects

public class Serialization {public static void main (string[] args) throws IOException, classnotfoundexception{
        Request R1 = new request ();
        R1.setaddressstatus (100);
        R1.setstatus (200);
        Request R2 = new request ();
        R2.setaddressstatus (300);

        R2.setstatus (400);

        File File = new file ("D:/mysql/workspace/spring/resources/request.ser");
        Request[] rs = deserializable (file);
    for (Request request:rs) System.out.println (request.getaddressstatus () + "" + request.getstatus ()); public static void serializable (file file, Request ... requests) throws IOException {ObjectOutputStream str
        EAM = new ObjectOutputStream (new FileOutputStream (file));
        if (requests!= null) Stream.writeobject (requests);
    Stream.Close (); public static request[] deserializable (file file) throws IOException, ClassNotFoundException {objectinputs Tream stream = new OBJECTINPUTSTReam (new FileInputStream (file));
        Request[] Requests = (request[]) stream.readobject ();
        Stream.Close ();
    return requests; }
}

The

will throw the following exception

Exception in thread "main" java.io.InvalidClassException:com.spring.domain.Request; Incompatible types for field addressstatus at Java.io.ObjectStreamClass.matchFields (objectstreamclass.java:2399) A T Java.io.ObjectStreamClass.getReflector (objectstreamclass.java:2293) at Java.io.ObjectStreamClass.initNonProxy ( objectstreamclass.java:741) at the Java.io.ObjectInputStream.readNonProxyDesc (objectinputstream.java:1876) at java.io. Objectinputstream.readclassdesc (objectinputstream.java:1745) at Java.io.ObjectInputStream.readOrdinaryObject ( objectinputstream.java:2033) at Java.io.ObjectInputStream.readObject0 (objectinputstream.java:1567) at Java.io.Obje Ctinputstream.readarray (objectinputstream.java:1966) at Java.io.ObjectInputStream.readObject0 ( objectinputstream.java:1561) at Java.io.ObjectInputStream.readObject (objectinputstream.java:427) at Com.spring.dom Ain. Serialization.deserializable (serialization.java:38) at Com.spring.domain.Serialization.main (SERIALIZATION.JAVA:24) 

Debug analysis, it is known that the deserialization type is int and the object's serialization type is integer, thus generating an exception.

Summary

The Spring Httpinvoker remote service is used in the project. The client and the server use a set of service interfaces, and the concrete implementation of the interface is on the server side.

When a client invokes a remote method, the (interface) method and parameters to be invoked are serialized and sent to the service side.

The server will deserialize the restore based on the request, call the concrete method to obtain the result, and serialize the result to the client.

The client receives the response and deserializes the server-side results into a restore.

This exception is generated if the client is inconsistent with the version of the server-side code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.