默认情况下包含Jackson JsonView属性

时间:2017-02-18 10:42:10

标签: java json spring serialization jackson

我有一些Pojos,其中包含一些我试图用JsonView注释限制的属性。但是当我没有用@JsonView注释弹簧端点时,带有@JsonView的属性会自动包含在序列化中。这是我的Pojo:

    @Entity @Table(name="user")
public class User implements Serializable {private static SecureRandom random = new SecureRandom();

    @Id
    @Getter
    @Setter
    @Column(name="id", nullable=false, updatable=false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    // This has to be capitalized to show up in the user details object
    private Long Id;

    @Getter
    @Setter
    @Column(name="created")
    private Timestamp created;

    @Getter
    @Setter
    @JsonView(gg.leet.model.Views.Admin.class)
    @Column(name="updated")
    private Timestamp updated;


    @Getter
    @Setter
    @JsonView(gg.leet.model.Views.Admin.class)
    @Column(name="email", unique=true, nullable=false)
    private String email;

    @Getter
    @Setter
    @Column(name="username", unique=true, nullable=false)
    private String username;

    @Getter
    @Setter
    @JsonIgnore
    @Column(name="password")
    private String password;

    @Getter
    @Setter
    @JsonView(gg.leet.model.Views.Admin.class)
    @Column(name="birthday")
    private Timestamp birthday;

    @Getter
    @Setter
    @JsonView(gg.leet.model.Views.Admin.class)
    @Column(name="gender")
    private String gender;

    @Getter
    @Setter
    @JsonView(gg.leet.model.Views.Admin.class)
    @Column(name="first_name")
    private String firstName;

    @Getter
    @Setter
    @JsonView(gg.leet.model.Views.Admin.class)
    @Column(name="last_name")
    private String lastName;

    @Getter
    @Setter
    @Column(name="avatar")
    private String avatar;

    @Getter
    @Setter
    @JsonView(gg.leet.model.Views.Admin.class)
    @Column(name="twitch_id")
    private String twitchId;

    @Getter
    @Setter
    @JsonIgnore
    @Column(name="twitch_access_token")
    private String twitchAccessToken;

    @Getter
    @Setter
    @JsonIgnore
    @Column(name="twitch_refresh_token")
    private String twitchRefreshToken;

    @Getter
    @Setter
    @JsonView(gg.leet.model.Views.Admin.class)
    @Column(name="facebook_id")
    private String facebookId;

    @Getter
    @Setter
    @JsonIgnore
    @Column(name="facebook_access_token")
    private String facebookAccessToken;

    @Getter
    @Setter
    @JsonIgnore
    @Column(name="facebook_token_expires")
    private Timestamp facebookTokenExpires;

    @Getter
    @Setter
    @JsonView(gg.leet.model.Views.Admin.class)
    @Column(name="steam_id")
    private String steamId;

    @Getter
    @Setter
    @JsonView(gg.leet.model.Views.Admin.class)
    @Column(name="sign_up_method")
    private String signUpMethod;

    @Getter
    @Setter
    @JsonView(gg.leet.model.Views.Admin.class)
    @Column(name="api_key")
    private String apiKey;

    @Getter
    @Setter
    @Column(name="points")
    private Integer points;

    @Getter
    @Setter
    @JsonIgnore
    @OneToMany(cascade=CascadeType.ALL, orphanRemoval=true, fetch=FetchType.EAGER)
    @JoinColumn(name="user_id", referencedColumnName="id")
    private Set<UserRole> roles;

    public User() { }

    public User(String email, String username, String password, Set<UserRole> roles) {
        this.email = email;
        this.username = username;
        this.password = password;
        this.roles = roles;
        this.apiKey = generateApiKey();
        this.points = 0;
        this.created = Timestamp.from(Instant.now());
    }

    public User(Long id, String email, String username, String password, Set<UserRole> roles) {
        this(email, username, password, roles);
        this.Id = id;
    }

    public User(User user) {
        this.Id = user.getId();
        this.created = user.getCreated();
        this.updated = user.getUpdated();
        this.email = user.getEmail();
        this.username = user.getUsername();
        this.password = user.getPassword();
        this.birthday = user.getBirthday();
        this.gender = user.getGender();
        this.firstName = user.getFirstName();
        this.lastName = user.getLastName();
        this.twitchId = user.getTwitchId();
        this.twitchAccessToken = user.getTwitchAccessToken();
        this.twitchRefreshToken = user.getTwitchRefreshToken();
        this.facebookId = user.getFacebookId();
        this.steamId = user.getSteamId();
        this.signUpMethod = user.getSignUpMethod();
        this.points = user.getPoints();
        this.avatar = user.getAvatar();
        // Copy roles list
        this.roles = user.getRoles().stream().collect(Collectors.toSet());
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + Id +
                ", email='" + email + '\'' +
                ", username='" + username + '\'' +
                ", twitchId='" + twitchId + '\'' +
                ", twitchAccessToken='" + twitchAccessToken + '\'' +
                ", twitchRefreshToken='" + twitchRefreshToken + '\'' +
                ", facebookId='" + facebookId + '\'' +
                ", steamId='" + steamId + '\'' +
                ", roles=" + roles +
                '}';
    }

    public static String generateApiKey() {
        return new BigInteger(130, random).toString(32);
    }
}

这里是带有注释掉的Regular类的控制器(当使用它时,@ JsonView注释过滤掉Pojos注释变量):

    /**
     * Get the leaderboard
     */
    // @JsonView(gg.leet.model.Views.Regular.class)
    @RequestMapping(value="/bingo/{roomId}/leaderboard", method=RequestMethod.GET, produces={ MediaType.APPLICATION_JSON_VALUE })
    public BingoLeaderboardMessage getLeaderboard(@PathVariable Long roomId) {
        return this.bingoLeaderboardService.getLeaderboardMessageForRoomId(roomId);
    }

Spring / Jackson的默认行为是否包含这些字段?如果是这种情况,那么由于必须在每个端点上使用注释,这不容易出错吗?

1 个答案:

答案 0 :(得分:0)

是的,这是默认行为。要将其关闭,您可以将其添加到您的应用程序属性中:

spring.jackson.mapper.default-view-inclusion=false