Alan Hay
Alan Hay

Reputation: 23246

Why serialized entity is wrapped with 'content' when using Lazy @ManyToOne?

While using Jackson's Hibernate4Module to deal with the serialization issues when dealing with a lazily loaded proxy in a Spring Data Rest project.

In general it solves the issue of Jackson trying to serialise uninitialized proxies however one side effect is that the JSON output differs:

Fetched directly: api/cases/5400

{
   "id": 5400,
   "practiceReference": "DWPYI9"
}

Fetched via a lazily loaded @ManyToOne: api/submissions/11901/parentCase

{
   "content": {
      "id": 5400,
      "practiceReference": "DWPYI9"
   }
}

Fetched via a non-lazily loaded @ManyToOne: api/submissions/11901/parentCase

{
   "id": 5400,
   "practiceReference": "DWPYI9"
}

As can be seen in the above, the JSON representation differs when serializing a lazy @ManyToOne association: the entity is wrapped in the content node.

If the association is non-Lazy then the same representation is written regardless of the path.

Is there a reason for this and can the additional "content" node somehow be prevented?


UPDATE FEB/2017:

I have found the same (deleted) question here:

https://stackoverflow.com/questions/33194554/two-different-resulting-jsons-when-serializing-lazy-objects-and-simple-objects

Which is referenced from this github issue. Also, it is reported here, so seems like a known issue:

Upvotes: 5

Views: 3312

Answers (0)

Related Questions