Home » Java » Use Gson to serialize a POJO

Use Gson to serialize a POJO

Posted by: admin December 28, 2021 Leave a comment


I use GSON serialize POJO — both the object before and after altered.

The altered one (call it A) which is setup by Struts2 could easily serialized to Json.

While the POJO before altered which is get from database via iBatis (call it B) couldn’t be serialized.

The error message says: Forgot to register a type adapter?

I have read the Gson API. But I don’t think register a type adapter for every POJO is a good idea.What makes the B couldn’t be serialized?

I write a clone() for my POJO, and the object cloned from B could be done also.

This is confusing… Is there anybody could answer me?

before altered(B’s clone):

{"id":"6429B5329C544711A9848AF243D10E4E","idType":"未选择","firstDate":"Feb 29, 2012 12:00:00 AM","name":"testetes","gender":"男","phone":"553223","city":"未选择","ocup":"未选择","nation":"未选择","famStru":"未选择","infSouc":"未选择","creater":"EE4783A6272A4B62A5CC68DB3C11FE1E","createDate":"Feb 29, 2012 12:00:00 AM","purpose":"未选择","education":"未选择","income":"未选择","cars":"未选择","acptCarpRent":"未选择","acptCarpPrice":"未选择","handStand":"未选择","intentHouse":"未选择","intentArea":"未选择","intentLayout":"未选择","nextDate":"Mar 7, 2012 12:00:00 AM","wuyeType":"未选择","attentionPro":"958B9E093A84415B901900C2DA25C712","ordinaryTraffic":"未选择","attentionPoint":"未选择","buyDate":"未选择","cityArea":"未选择","lastUpdate":"Feb 29, 2012 12:00:00 AM","lastModifier":"EE4783A6272A4B62A5CC68DB3C11FE1E","saler":"A4FB4877DC2945E980477544A955B57F","state":"意向","status":"0"}

After altered(A):

{"id":"6429B5329C544711A9848AF243D10E4E","idType":"未选择","firstDate":"Feb 29, 2012 12:00:00 AM","visitMode":"","name":"testetes","gender":"男","telPhone":"","phone":"553223","fax":"","adrs":"","postCode":"","email":"","workUnit":"","city":"未选择","media_id":"","ocup":"未选择","idNum":"","nation":"未选择","famStru":"未选择","infSouc":"未选择","createDate":"Feb 29, 2012 12:00:00 AM","idAdr":"","purpose":"未选择","education":"未选择","income":"未选择","cars":"未选择","acptCarpRent":"未选择","acptCarpPrice":"未选择","handStand":"未选择","intentHouse":"未选择","intentArea":"未选择","intentLayout":"未选择","customerDetail":"","wuyeType":"未选择","attentionPro":"958B9E093A84415B901900C2DA25C712","ordinaryTraffic":"未选择","attentionPoint":"未选择","buyDate":"未选择","cityArea":"未选择","lastUpdate":"Mar 11, 2012 2:58:04 PM","lastModifier":"00000000000000000000000000000000","saler":"A4FB4877DC2945E980477544A955B57F","state":"意向"}

It sounds like your POJO is of type Customer? When you clone your object, you’re creating a new Customer, and Gson can serialize that just fine. When you fetch that same object from the DB, however, it’s not a standard Customer object. Instead, it’s a subclass that includes some persistence information, such as the class of the object.

Probably the simplest solution is to use Gson’s @Expose annotation. If you create your Gson object with new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(), then you can mark each of the Customer fields that you would like to serialize with @Expose. Any other fields, including those of your persistence framework’s subclass, will not be serialized.


Brandon was right. Here is another solution if you don’t want to use any annotation or modify your POJO class. This may help for any other guys.

Type typeOfSrc = new TypeToken<A>() {}.getType(); //this helps for generic one.
gson.toJson(obj, typeOfSrc); or gson.toJson(obj, A.class);