Demand:
As it usually writes some data back to the front end, however, often the situation is:
Only a subset of the field properties are required, so sometimes you have to do something to ensure that only the fields that you need are returned
In particular, the interface to the mobile terminal or other interfaces that take the HTTP protocol require some attributes within the object
Scheme:
It is convenient to take advantage of the @jsonview annotations. He can control the output of the field properties we want to serialize
Simple and easy to understand
public class View {interface Summary {}}public class User {@JsonView (View.Summary.class) private Long ID; @JsonView (view . Summary.class) private string FirstName; @JsonView (View.Summary.class) private string lastname;private string email; private string Address;private string Postalcode;private string city;private string country;} public class Message {@JsonView (View.Summary.class) private Long ID; @JsonView (View.Summary.class) Private localdate Created; @JsonView (View.Summary.class) private String title; @JsonView (View.Summary.class) Private User author;private List<user> recipients; Private String body;}
@RestControllerpublic class Messagecontroller {@Autowiredprivate messageservice messageservice; @RequestMapping ("/") Public list<message> GetAllMessages () {return Messageservice.getall ();} @RequestMapping ("/{id}") Public Message getMessage (@PathVariable Long ID) {return messageservice.get (ID);}}
In the above example, only the GetAllMessages () method has a @jsonview (View.Summary.class) annotation, so its request will only show the field properties that are annotated to serialize
Results of responses: Only annotated properties appear
[ { "id" : 1, "created" : "2014-11-14", "title " : " Info ", " author " : { " id " : 1, "FirstName" : "Brian", "LastName" : "Clozel" }}, { "id" : 2, "created" : "2014-11-14", " Title " : " Warning ", " author " : { " id " : 2, "FirstName" : "Stéphane", "LastName" : " Nicoll " }}, { " id " : 3, " created " : " 2014-11-14 ", "title" : "Alert", "author" : { "id" &NBSP;: 3, "FirstName" : "Rossen", "LastName" &NBSP;: "Stoyanchev" }} ]
If you call GetMessage (@PathVariable Long ID) This method returns the result of the data:
All message properties for all fields
{ "id" : 1, "created" : "2014-11-14", "title" &NBSP;: "Info", "Body" : "This is an information message", " Author " : { " id " : 1, " firstname "&NBSP;: "Brian", "LastName" : "Clozel", "email" &NBSP;: "[email protected]", "Address" : "1 jaures street", "PostalCode" : "69003", "City" : "Lyon", "Country" : "France" }, "Recipients" : [ { "id" : 2, "FirstName" : "Stéphane", "LastName" : "Nicoll", "email" : "[email protected] ", " Address " : " 42 obama street ", " PostalCode " : " 1000 ", "City" : "Brussel", "Country" : "Belgium" }, { "id" : 3, "firstname" &NBSP;: "Rossen", "LastName" : "Stoyanchev", "email" : "[email protected]", "Address" : "3 warren street ", " PostalCode " : " 10011 ", " City " : " New york ", " Country " : " USA "&NBSP;&NBSP;}&NBSP;]}
In addition @JsonView annotations are also supported for inheritance
public class view {interface summary {}interface summarywithrecipients extends summary {}}public class message {@JsonView (View.Summary.class) Private Long id; @JsonView (View.Summary.class) private localdate created; @JsonView ( View.Summary.class) private string title; @JsonView (View.Summary.class) private user author;@ Jsonview (View.SummaryWithRecipients.class) private list<user> recipients; private string body;} @RestControllerpublic class messagecontroller {@Autowiredprivate MessageService Messageservice, @JsonView (View.SummaryWithRecipients.class) @RequestMapping ("/with-recipients") public list <message> getallmessageswithrecipients () {return messageservice.getall ();}}
The Recipients attribute is more in the result
{"id": 1, "created": "2014-11-14", "title": "Info", "Body": "This was an information message", "author": {"I D ": 1," FirstName ":" Brian "," LastName ":" Clozel ",}," recipients ": [{" id ": 2," FirstName ":" Stéphane "," LastName ":" Nicoll ",}, {" id ": 3," FirstName ":" Rossen "," LastName ":" Stoyanchev ",}]}
SPRINGMVC Jsonview Note Notes