Home > Blockchain >  When object needs to be serialized in spring boot
When object needs to be serialized in spring boot

Time:12-30

When I am sending some data using the RestTemplate to other microservice, does that request object and response object needs to be serialized?

CodePudding user response:

yes(Especially when transferring objects between services)! and you must know why we should do this! think about this, you and me work in same team, you shoule sending some data using the RestTemplate to my service, now each of us use a same class Person like this:

public class Person {
    public String sex;
    public String toString() {
        return this.sex;
    }
}

If, objects are allowed to be passed without serialization,cause I don't like maintaining the same class with you, I created a new Person class myself。

but some day,However, due to changing requirements, your Person class modified,like this:

public class Person {
    public String sex;
    public String toString() {
        return this.sex == "man" ? "woman" : "man";
    }
}

Now there's a mistake between us。

but if serialized! every time we change the class Person, we change the serialVersionUID, we can make sure we use the same class\Object to use(Because, even if I create the Person class separately, since the serialVersionUID is modified every time, when the original Person changes, I can’t use my Person to replace it, so I have to get the original Person’s serialVersionUID. At this time, I can see your modification, and I will synchronize to my own class to avoid mistakes).

CodePudding user response:

Yes, when using RestTemplate all objects are serialized,deserialized in process.

  • Related