2017-02-18 4 views
1

JsonViewアノテーションで制限しようとしているいくつかのプロパティを持つPojosがあります。しかし、@JsonViewで春の終点に注釈を付けることができない場合、@JsonViewのプロパティは自動的に逐次化に含まれます。ここに私のPOJOです:ここでは Jackson JsonViewのプロパティはデフォルトで組み込まれています

@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アノテーションはPOJOの注釈付きの変数を除外し):

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

がスプリングによってデフォルトの動作です/ジャクソンにこれらのフィールドを含める?この場合、アノテーションはすべてのエンドポイントで使用する必要があるため、このエラーは発生しにくいのでしょうか?

答えて

0

はい、これがデフォルトの動作です。これをオフにするには、アプリケーションのプロパティに次のように追加します。

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

またはmapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION、false); –

関連する問題