您的位置:首页 > Web前端 > JavaScript

Jackson JsonView

2016-02-27 00:00 706 查看
摘要: Jackson JsonView

在Spring MVC -> ResponseEntity,有One2Many等映射,一般会标注@JsonIgnore,在返回时忽略级联关系,但是有些场景会要求返回级联对象,所以@JsonIgnore是不满足需求的,所以引入了@JsonView,在控制层动态的指定返回字段。

0Controller中:

@RestController
public class UserController {
@RequestMapping(value = "/user", method = RequestMethod.GET)
@JsonView(User.WithoutPasswordView.class)
public User getUser() {
return new User("eric", "7!jd#h23");
}
}


public class User {
public interface WithoutPasswordView {};
public interface WithPasswordView extends WithoutPasswordView {};
private String username;
private String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
@JsonView(WithoutPasswordView.class)
public String getUsername() {
return this.username;
}
@JsonView(WithPasswordView.class)
public String getPassword() {
return this.password;
}
}


参考:http://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/htmlsingle/#mvc-ann-jsonview
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: