jsonView 简单实用实例

2017-11-05  本文已影响13人  lucode
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<User> query(){
        List<User> 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;
    }
}
View image.png
上一篇下一篇

猜你喜欢

热点阅读