在多个类中使用JsonFilter

时间:2018-08-22 11:36:50

标签: java spring spring-boot jackson

我想创建一个过滤器来过滤我的JSON响应。我使用JSON过滤器成功完成了此操作,但现在调用另一个具有实体属性的控制器时遇到一个问题。

我有一个ForumUser班:

@Data
@Entity
@NoArgsConstructor(force = true, access = AccessLevel.PUBLIC)
@Table(name = "forum_user")
@JsonFilter("forumuser_filter")
public class ForumUser {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonProperty("id")
    private UUID id;

    @JsonProperty("first_name")
    @Column(name = "first_name")
    private String firstName;

    @JsonProperty("last_name")
    @Column(name = "last_name")
    private String lastName;

    @JsonProperty("phone_number")
    @Column(name = "phone_number")
    private String phoneNumber;

    @Column(name = "username", unique = true)
    @NotNull
    @JsonProperty("username")
    private String username;

    // More attributes omitted
}

和类Category

@Data
@Entity
@NoArgsConstructor(force = true, access = AccessLevel.PUBLIC)
@Table(name = "category")
@JsonFilter("category_filter")
public class Category {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonProperty("id")
    private UUID id;

    @JsonProperty("name")
    @Column(name = "name", unique = true)
    private String name;

    @JsonProperty("description")
    @Column(name = "description")
    private String description;

    @JsonProperty("created_at")
    @Column(name = "created_at", updatable = false)
    @CreationTimestamp
    private Timestamp createdAt;

    @JsonProperty("deactivated_at")
    @Column(name = "deactivated_at")
    private Timestamp deactivatedAt;

    @JsonProperty("created_by_id")
    @JoinColumn(name = "created_by_id")
    @OneToOne
    private ForumUser createdBy;

    @JsonProperty("deactivated_by_id")
    @JoinColumn(name = "deactivated_by_id")
    @OneToOne
    private ForumUser deactivatedBy;
}

当我调用类别控制器时,出现此错误:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot resolve PropertyFilter with id 'forumuser_filter'; no FilterProvider configured (through reference chain: java.util.ArrayList[6]->com.example.demo.DTOs.CategoryDTO["createdBy"])

我知道有一个类似的问题,但是我不明白在哪里添加对象映射器以及如何在需要时实现它。

0 个答案:

没有答案