As the title of this article says, you can define different views of the object which you want to serialize, typically the response of a REST service.
For example, you can define a view called Summary and at the same time another called Complete. Le't me show you with a code example.
First of all, you have to define, in your DTO, the required views. In this case we have two views, MessageSummary and MessageEntire.
Note that the class used to define your views is an interface which includes other interfaces. Also is important to note, that the MessageEntire view extends from MessageSummary, then all fields marked with MessageSummary are included in the view MessageEntire by inheritance.
Finally, from your REST controller you can choose the required view by the response. For example, in our case, when we construct a new message, it doesn't have sense to return the likes fields because always is equals to zero, otherwise it has sense when the GET method is invoked.
Jackson Power!
No comments:
Post a Comment