Properti Jackson JSONView disertakan secara default

Saya memiliki beberapa Pojo dengan beberapa properti yang saya coba batasi dengan anotasi JsonView. Namun ketika saya gagal memberi anotasi pada titik akhir pegas dengan @JsonView, properti dengan @JsonView secara otomatis disertakan dalam serialisasi. Ini Pojo saya:

    @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);
    }
}

Inilah pengontrol dengan kelas Reguler yang dikomentari (anotasi @JsonView memfilter variabel beranotasi Pojos saat ini digunakan):

    /**
     * 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);
    }

Apakah perilaku default Spring/Jackson menyertakan bidang ini? Jika demikian, bukankah ini rawan kesalahan karena anotasi harus digunakan di setiap titik akhir?


person AndrewD    schedule 18.02.2017    source sumber


Jawaban (1)


Ya, ini adalah perilaku default. Untuk mematikannya, Anda dapat menambahkan ini ke properti aplikasi Anda:

spring.jackson.mapper.default-view-inclusion=false
person Vale H    schedule 22.06.2017
comment
atau mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false); - person Antoine Meyer; 09.07.2017