คุณสมบัติ Jackson JsonView รวมอยู่ตามค่าเริ่มต้น

ฉันมี 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);
    }
}

นี่คือคอนโทรลเลอร์ที่มีคลาสปกติใส่เครื่องหมายความคิดเห็น (คำอธิบายประกอบ @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 รวมฟิลด์เหล่านี้หรือไม่ หากเป็นกรณีนี้ ข้อผิดพลาดนี้ไม่น่าจะเกิดขึ้นเนื่องจากต้องใช้คำอธิบายประกอบกับทุกจุดปลายทางใช่หรือไม่


person AndrewD    schedule 18.02.2017    source แหล่งที่มา


คำตอบ (1)


ใช่ นี่เป็นพฤติกรรมเริ่มต้น หากต้องการปิด คุณสามารถเพิ่มสิ่งนี้ลงในคุณสมบัติแอปพลิเคชันของคุณได้:

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