Customizing the JSON output
Using our tools we are able to easily see the request generated by our server. It is huge. By default, Jackson, the JSON serialization library used by Spring Boot, will serialize everything that is accessible with a getter method.
We would like something lighter, such as this:
{ "text": "original text", "user": "some_dude", "profileImageUrl": "url", "lang": "en", "date": 2015-04-15T20:18:55, "retweetCount": 42 }
The easiest way to customize which fields will be serialized is by adding annotations to our beans. You can either use the @JsonIgnoreProperties
annotation at the class level to ignore a set of properties or add @JsonIgnore
on the getters of the properties you wish to ignore.
In our case, the Tweet
class ...
Get Spring MVC: Designing Real-World Web Applications now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.