Restful resources do not always have a one-to-one mapping with your jpa entities. As I see it there are a few problems that I am trying to figure out how to handle:
- When a resource has information that is populated and saved by more than one entity.
- When an entity has more information in it that you want to send down as a resource. I could just use Jackson's
@JsonIgnore
but I would still have issue 1, 3 and 4. - When an entity (like an aggregate root) has nested entities and you want to include part of its nested entities but only to a certain level of nesting as your resource.
- When you want to exclude once piece of an entity when its part of one parent entity but exclude a separate piece when its part of a different parent entity.
- Blasted circular references (I got this mostly working with JSOG using Jackson's
@JsonIdentityInfo
)
Possible solutions: The only way I could think of that would handle all of these issues would be to create a whole bunch of "resource" classes that would have constructors that took the needed entities to construct the resource and put necessary getters and setters for that resource on it. Is that overkill?
To solve 2, 3, 4 , and 5 I could just do some pre and post processing on the actual entity before sending it to Jackson to serialize or deserialize my pojo into JSON, but that doesn't address issue 1.
These are all problems I would think others would have come across and I am curious what solutions other people of come up with. (I am currently using JPA 2, Spring MVC, Jackson, and Spring-Data but open to other technologies)