jsonView 简单实用实例

import com.fasterxml.jackson.annotation.JsonView;

/**
 * @author yunfeng.lu
 * @create 2017/11/5.
 */
public class User {
    public interface PrintUser{}
    public interface PrintUserDetail extends PrintUser{}

    private String name;
    private String password;

    @JsonView(PrintUser.class)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @JsonView(PrintUserDetail.class)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

@RestController
@RequestMapping("/user")
public class UserController {

     // 只会去打印 name  如下图所示
    @GetMapping("/query")
    @JsonView(User.PrintUser.class)
    public List query(){
        List list=new ArrayList<>();
        list.add(new User());
        list.add(new User());
        list.add(new User());
        return list;
    }
    // name 和 password 都会打印
    @GetMapping("/get/info/{id:\\d+}")
    @JsonView(User.PrintUserDetail.class)
    public User getUserInfo(){
        User user=new User();
        user.setName("lucode");
        return user;
    }
}
jsonView 简单实用实例_第1张图片
View
jsonView 简单实用实例_第2张图片
image.png

你可能感兴趣的:(jsonView 简单实用实例)