美文网首页
spring boot Jsonview 控制输入输出后的jso

spring boot Jsonview 控制输入输出后的jso

作者: 破地瓜 | 来源:发表于2018-12-17 21:55 被阅读47次

我们在对一个对象进行查询时,可能会用多个查询功能
比如查询用户时
用户类如下

@Data
public class User {
    private Integer id;
    private String name;
    private Integer age;
    private String password;
    private String phone;
}

有两个需求

  • 查询列表,要求不显示passwordphone
  • 根据ID查询时,不显示password
    Controller代码如下
@RestController
    public class DemoController{
        @GetMapping("/list")
        public List<User> list(){
             return User.buildList();
        }
        @GetMapping("/{id}")
        public User getById(@PathVariable("id") Integer id){
            return User.build(id);
        }
    }

返回结果 为所有字段

[
    {
        "id": 0,
        "name": "Lucy",
        "age": 0,
        "password": "password0",
        "phone": "1310"
    },
    {
        "id": 1,
        "name": "Lucy",
        "age": 1,
        "password": "password1",
        "phone": "1311"
    },
    {
        "id": 2,
        "name": "Lucy",
        "age": 2,
        "password": "password2",
        "phone": "1312"
    }
]

@JsonView注解可以完美解决此问题
修改User 如下

@Data
public class User {
    public static interface BaseUser{}
    public static interface ListUser extends  BaseUser{};
    public static interface SingleUser  extends  ListUser{};
    @JsonView(BaseUser.class)
    private Integer id;
    @JsonView(BaseUser.class)
    private String name;
    @JsonView(BaseUser.class)
    private Integer age;
    @JsonView(SingleUser.class)
    private String password;
    @JsonView(ListUser.class)
    private String phone;
|

修改Controller代码

@RestController
    public class DemoController{
        @GetMapping("/list")
        @JsonView(User.ListUser.class)
        public List<User> list(){
             return User.buildList();
        }
        @GetMapping("/{id}")
        @JsonView(User.SingleUser.class)
        public User getById(@PathVariable("id") Integer id){
            return User.build(id);
        }
    }

调用list方法

curl --request GET \
  --url http://localhost:8080/list \
  --header 'Content-Type: application/x-www-form-urlencoded' \
  --header 'Postman-Token: 466e0918-14d6-4431-948c-0abfebddbe70' \
  --header 'cache-control: no-cache' \
  --data undefined=

返回

[
    {
        "id": 0,
        "name": "Lucy",
        "age": 0,
        "phone": "1310"
    },
    {
        "id": 1,
        "name": "Lucy",
        "age": 1,
        "phone": "1311"
    },
    {
        "id": 2,
        "name": "Lucy",
        "age": 2,
        "phone": "1312"
    }
]

调用getById方法

curl --request GET \
  --url http://localhost:8080/1 \
  --header 'Postman-Token: 647e0e38-e3b8-4727-956a-60bd2a3c1924' \
  --header 'cache-control: no-cache'

返回

{
    "id": 1,
    "name": "Lucy",
    "age": 1,
    "password": "password1",
    "phone": "1311"
}

实现返回了同一对象的不同视图
代码地址:
https://github.com/jiabiaoli/demo/tree/master/jsonview

相关文章

网友评论

      本文标题:spring boot Jsonview 控制输入输出后的jso

      本文链接:https://www.haomeiwen.com/subject/jzlhkqtx.html