使用@JsonView注解控制返回的Json属性

我也是刚看到原来还可以这么玩,但是我还是习惯使用Dto,我总感觉这样做的话实体类耦合程度有点高。还是记录以下,万一今后用到了呢

⒈在实体类中使用接口来声明该实体类的多个视图。

⒉在实体类的属性get方法上指定该属性在那个视图中呈现。

 1 package cn.coreqi.security.entities;
 2 
 3 import com.fasterxml.jackson.annotation.JsonView;
 4 
 5 public class User {
 6     public interface UserSimpleView{};  //简单视图
 7     public interface UserDetailView extends UserSimpleView{};  //高级视图
 8 
 9     private Long id;
10     private String username;
11     private String password;
12     private Integer enabled;
13 
14     public User() {
15     }
16 
17     public User(Long id, String username, String password, Integer enabled) {
18         this.id = id;
19         this.username = username;
20         this.password = password;
21         this.enabled = enabled;
22     }
23 
24     @JsonView(UserSimpleView.class)
25     public Long getId() {
26         return id;
27     }
28 
29     public void setId(Long id) {
30         this.id = id;
31     }
32 
33     @JsonView(UserSimpleView.class)
34     public String getUsername() {
35         return username;
36     }
37 
38     public void setUsername(String username) {
39         this.username = username;
40     }
41 
42     @JsonView(UserDetailView.class)
43     public String getPassword() {
44         return password;
45     }
46 
47     public void setPassword(String password) {
48         this.password = password;
49     }
50     @JsonView(UserSimpleView.class)
51     public Integer getEnabled() {
52         return enabled;
53     }
54 
55     public void setEnabled(Integer enabled) {
56         this.enabled = enabled;
57     }
58 
59     @Override
60     public String toString() {
61         return "User{" +
62                 "id=" + id +
63                 ", username='" + username + '\'' +
64                 ", password='" + password + '\'' +
65                 ", enabled=" + enabled +
66                 '}';
67     }
68 }

⒊在控制器的Action方法上使用@JsonView注解声明该Action返回的实体类视图。

 1 package cn.coreqi.security.controller;
 2 
 3 import cn.coreqi.security.entities.User;
 4 import com.fasterxml.jackson.annotation.JsonView;
 5 import org.springframework.web.bind.annotation.GetMapping;
 6 import org.springframework.web.bind.annotation.PathVariable;
 7 import org.springframework.web.bind.annotation.RestController;
 8 import java.util.ArrayList;
 9 import java.util.List;
10 
11 @RestController
12 public class UserController {
13     @GetMapping("/users")
14     @JsonView(User.UserSimpleView.class)
15     public List<User> query(){
16         List<User> users = new ArrayList<>();
17         users.add(new User(1L,"fanqi","fanqi",1));
18         users.add(new User(2L,"fanqi","fanqi",1));
19         users.add(new User(3L,"fanqi","fanqi",1));
20         return users;
21     }
22 
23     @GetMapping("/user/{id:\\d+}")  //使用正则指定Id为数字
24     @JsonView(User.UserDetailView.class)
25     public User getInfo(@PathVariable String id){
26         return new User(1L,"fanqi","fanqi",1);
27     }
28 }

 

posted @ 2019-03-26 13:09  SpringCore  阅读(777)  评论(0编辑  收藏  举报