转自:
下文是世上最通俗易懂的JsonView注解,如下所示
@JsonView是jackson json中的一个注解
Spring mvc也支持JsonView注解
JsonView注解功能:
设置在json字符串输出时
是否输入及输出指定的字段
例:
Controller返回时---不输出密码
1.定义两个接口UserSimpleView和UserDetailView 2.在get方法上使用JsonView设置相应的接口类 public class User { public interface UserSimpleView {}; public interface UserDetailView extends UserSimpleView {}; private String name; private String password; @JsonView(UserSimpleView.class) public String getNname (){ return name; } public void setName (String name){ this.name = name; } @JsonView(UserDetailView.class) public String getPassword (){ return password; } public void setPassword (String password){ this.password = password; } } controller方法使用视图 @RequestMapping(value="/testSimpleView/{id:\\d+}",method=RequestMethod.GET) @JsonView(User.UserSimpleView.class) public User testSimpleView(@PathVariable(value="id")String number) { User user=new User(); user.setPassword(number); user.setName("java265.com--username"); return user; } @RequestMapping(value="/testDetailView/{id:\\d+}",method=RequestMethod.GET) @JsonView(User.UserDetailView.class) public User testDetailView(@PathVariable(value="id")String number) { User user=new User(); user.setPassword(number); user.setName("java265.com--username"); return user; } //testSimpleView 不会返回密码 {"name":"java265.com--username"} //testDetailView 会返回密码 { {“name”:"java265.com--username",”password”:”*******”}}