【问题标题】:Jackson @JsonIgnoreProperties seems not to work all the timeJackson @JsonIgnoreProperties 似乎并不总是有效
【发布时间】:2019-11-19 02:55:03
【问题描述】:

我将两个实体映射到以下类:

@Getter
@Entity
@Table(name = "users")
@JsonIdentityInfo(generator = PropertyGenerator.class,
                  property  = "id")
@SequenceGenerator(name = "id-generator", sequenceName = "seq_users")
@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false)
@NoArgsConstructor(access = PROTECTED)
@RequiredArgsConstructor
public class User extends IdentifiedById {

    @Include
    @NonNull
    @Column(name = "email_address", unique = true)
    private String emailAddress;

    @Setter
    @JsonIgnore
    private String hash;

    @Setter
    private boolean admin;

    @OneToMany(
            mappedBy = "user",
            orphanRemoval = true,
            cascade = ALL
    )
    @JsonIgnoreProperties("user")
    private Set<Cart> carts;

    {
        carts = new HashSet<>(0);
    }
}

@Getter
@Entity
@Table(
        name = "carts",
        uniqueConstraints = @UniqueConstraint(
                columnNames = {
                        "creation_time",
                        "user_id"
                }
        )
)
@JsonIdentityInfo(generator = PropertyGenerator.class,
                  property  = "id")
@SequenceGenerator(
        name = "id-generator",
        sequenceName = "seq_carts"
)
@EqualsAndHashCode(
        callSuper = false
)
@RequiredArgsConstructor
@NoArgsConstructor(access = PROTECTED)
public class Cart extends IdentifiedById {


    @NonNull
    @Column(name = "creation_time")
    private LocalDateTime creationTime;

    @NonNull
    @ManyToOne(cascade = ALL)
    @JoinColumn(
            name = "user_id",
            referencedColumnName = "id"
    )
    @JsonManagedReference
    private User user;


    @Exclude
    @JsonProperty("productStoreQuantities")
    @JsonSerialize(converter = AdditionConverter.class)
    @OneToMany(mappedBy = "cart", orphanRemoval = true, cascade = ALL)
    private Set<Addition> additions;

    {
        additions = new HashSet<>(0);
    }
}

如果我检索一个用户,它的购物车不包含它的引用,这对我来说很好。 现在从休息端点的角度来看,如果有人像这样请求多个用户,我不想序列化用户及其购物车:

**/api/users -> {"id":1, "emailAddress":"test@test.test", "admin": false}**

**/api/users/1 -> {"id":1, "emailAddress":"test@test.test", "admin": false, "carts": [...]}**

因此,我创建了一个名为 Users 的包装类,其中包含一个用 @JsonValue 和 @JsonIgnoreProperties("carts") 注释的用户列表:

@RequiredArgsConstructor
public class Users implements Serializable, List<User> {

    @Delegate
    @JsonValue
    @JsonIgnoreProperties("carts")
    private final List<User> values;
}

我不知道为什么,但是购物车一直被序列化,我听说 @JsonIgnoreProperties 不适用于集合和数组,但在我的第一种情况下可以。

【问题讨论】:

    标签: spring jpa serialization jackson


    【解决方案1】:

    您应该在类级别使用 JsonIgnoreProperties。 这在这篇文章中有很好的解释

    https://www.baeldung.com/jackson-ignore-properties-on-serialization

    【讨论】:

      猜你喜欢
      • 2011-11-15
      • 2020-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      • 1970-01-01
      相关资源
      最近更新 更多