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

你可能感兴趣的:(Jackson JsonView)