-
Notifications
You must be signed in to change notification settings - Fork 1
模型字段忽略
阿东 edited this page May 5, 2020
·
3 revisions
在controller中,想要隐藏部分字段又不想写新的模型,这两个注解可以帮助你快速实现
@IgnoresField
参数名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
type | Class<?> | 指定类型 | |
include | String[] | 保留哪些字段 | 空 |
filter | String[] | 过滤哪些字段 | 空 |
@IgnoresFields
参数名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
value | IgnoresField [] | 过滤组 | 空 |
用法
1.新建一个实体类
public class Result<T> {
private Integer code;
private T data;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
2.新建第二个实体
public class User {
/**
* 名称
*/
private String name;
/**
* 年龄
*/
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
3.在controller忽略指定值
@GetMapping("s")
@ResponseBody
@IgnoresField(type = Result.class,include = {"data"})
public Result get(@RequestParam("m")String m){
Result<String> result =new Result<String>();
result.setCode(2);
result.setData("1111");
return result;
}
4.响应如下
{
"code": null,
"data": "1111"
}
5.组合用法
@GetMapping("s")
@ResponseBody
@IgnoresFields({
@IgnoresField(type = Result.class,include = {"data"}),
@IgnoresField(type = User.class,include = {"age"})
})
public Result get(@RequestParam("m")String m){
Result<User> result =new Result<User>();
result.setCode(2);
User user = new User();
user.setAge(60);
user.setName("小明");
result.setData(user);
return result;
}
6.响应如下
{
"code": null,
"data": {
"age": 60
}
}