这篇文章将会讲述如何使用@JsonView
过滤给前台数据的输出在序列化的上下文中,包括集合和单字段。
例子
这个例子中我们将会用”summary” View 来标志被序列化的字段。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| 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; }
|
@JsonView
这个注解应用非常灵活。这个注解我们可以加在一个Controlelr的方法上,这个方法的返回的类中的字段就会被@JsonView
序列化。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @RestController public class MessageController { @Autowired private MessageService messageService; @JsonView(View.Summary.class) @RequestMapping("/") public List<Message> getAllMessages() { return messageService.getAll(); } @RequestMapping("/{id}") public Message getMessage(@PathVariable Long id) { return messageService.get(id); } }
|
看这段代码我们知道,getAllMessages()
方法是retrieved
所有的message,但是返回的时侯每个类中只会返回我们加@JsonView
注解的字段。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| [ { "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" : 3, "firstname" : "Rossen", "lastname" : "Stoyanchev" } } ]
|
在Spring MVC的default configuration,MapperFeature.DEFAULT_VIEW_INCLUSION
是为false
,这意味着,当我们使用JSON View的时候,没有被注解的fields or properties body
recipients
将不会被序列化。
当然,当我们使用getMessage()
retrieved一个明确的message
的时候,因为方法中没有使用JSON View,所以所有的字段将被序列化。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| { "id" : 1, "created" : "2014-11-14", "title" : "Info", "body" : "This is an information message", "author" : { "id" : 1, "firstname" : "Brian", "lastname" : "Clozel", "email" : "bclozel@pivotal.io", "address" : "1 Jaures street", "postalCode" : "69003", "city" : "Lyon", "country" : "France" }, "recipients" : [ { "id" : 2, "firstname" : "Stéphane", "lastname" : "Nicoll", "email" : "snicoll@pivotal.io", "address" : "42 Obama street", "postalCode" : "1000", "city" : "Brussel", "country" : "Belgium" }, { "id" : 3, "firstname" : "Rossen", "lastname" : "Stoyanchev", "email" : "rstoyanchev@pivotal.io", "address" : "3 Warren street", "postalCode" : "10011", "city" : "New York", "country" : "USA" } ] }
|
一个类或接口可以使用@JsonView
,但是有的时候我们会经常遇到@OneToMany
or @ManyToMany
,但是我么可以用继承来解决集合或List的问题。例如,在这个例子中我们会看到private List<User> recipients;
,我们用的注解不是@JsonView(View.Summary.class)
而是继承他的注解@JsonView(View.SummaryWithRecipients.class)
来完成,在下面的这个方法中的字段将会被@JsonView(View.Summary.class)
和 @JsonView(View.SummaryWithRecipients.class)
共同序列化。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| 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; } @RestController public class MessageController { @Autowired private MessageService messageService; @JsonView(View.SummaryWithRecipients.class) @RequestMapping("/with-recipients") public List<Message> getAllMessagesWithRecipients() { return messageService.getAll(); } }
|