Hi, how do you do this elegantly say using Jackson or maybe Gson? If you are writing a web based commenting system with 2 pages only:
1) Main page showing comments and the name of the user who posted each comment.
2) Member detail page showing all information such as join date, post count, last posted date, etc for any given user.
You naturally have a situation where you'd need to serve different amount of information for the same User object depending on which page you are serving.
If I use something like Spring Data's findAll() method (ie commentRepository.findAll()), I'll be able to get obviously all the comments (assuming we have a small system here) and the users (via predefined @OneToOne annotation).
If I then serialize to JSON using Jackson, I should see something like the following.
Then if I use the following Handlebars template, I'd be able to show just only the name of the person who posted the comment, and the rest of the user details such as id, totalPosts, lastPostedOn would never even hit the browser.
And then on the other page I can use something like the following to show all the details I want for each user.
So far so good. I can use the same User model and display different level of detail depending on what I specify in the template.
But this only works if you are using a template that gets rendered on the server such as handlebars.java. What if you now have a template that gets rendered on the client side such as handlebars.js? If we are running the same handlebars template on the browser, the server will send all fields from the User model (ie all columns per user from the database) to the client before it gets picked out by the template on which one to use. The disadvantages are:
1) Poor performance because we are sending unnecessarily large amount of traffic to the browser.
2) Unable to hide details because if the User object contains confidential fields such as e-mail address, phone numbers, etc we don't want to send all these to the browser even if they won't be displayed at the end.
So the question is how can we do this properly? Has anyone come across this scenario? I'm hoping that I may find a way to have the Jackson 2 library to serialize only some of the fields to JSON in one case and all of the fields to JSON in another case depending on a flag which indicates which page we're serving but initial research doesn't seem like this is supported. There are custom serializers but this seems to be a bit complicated for a scenario that is supposed to be quite common. Do I have to write two User model classes now using two different native queries (one selecting some columns and another selecting more columns)? What would you do?
The only sensible way I can see is really resorting to using multiple Java classes to represent the same thing, and use the correct one depending on the scenario. For our example case, we'd have a User class that has all the fields and will be used for the user list page where we want to show all details. Then if we want to serve the comments page, we'd have to retrieve the details from database like how we normally would, and then convert it to a trimmed down class before serializing to JSON.