【问题标题】:Jackson @JsonIgnoreProperties not working from super class杰克逊@JsonIgnoreProperties 不在超级班工作
【发布时间】:2019-10-18 06:42:15
【问题描述】:

我有一个名为“BaseEntity”的类。从基本实体扩展的其他实体。

基础实体

@MappedSuperclass
public abstract class BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @ManyToOne
    @JsonIgnoreProperties({"createdBy", "lastModifiedBy", "manager"})
    private User createdBy;

    private Instant createdDate = Instant.now();

    @ManyToOne
    @JsonIgnoreProperties({"createdBy", "lastModifiedBy", "manager"})
    private User lastModifiedBy;

    private Instant lastModifiedDate = Instant.now();

    // getters & setters
}

从基本实体扩展的用户实体。

@Entity
public class User2 extends BaseEntity {

    private String userName;

    @ManyToOne
    @JsonIgnoreProperties({"createdBy", "lastModifiedBy", "manager"})
    private User manager;

    // getters & setters
}

错误 当我尝试序列化用户时出现此错误。

org.springframework.http.converter.HttpMessageConversionException:类型定义错误:[简单类型,类a.b.c.d.domain.User];嵌套异常是 com.fasterxml.jackson.databind.exc.InvalidDefinitionException: 直接自引用导致循环(通过引用链:java.util.Collections$UnmodifiableRandomAccessList[0]->a.b.c.d.domain.User["createdBy"])

如果我像这样只更改 createdBy 和 lastModifiedBy 就可以了。管理员用户不会导致错误

    @ManyToOne
    @JsonIgnore
    private User createdBy;

但这次 json 不包含任何 createdBy 或 lastModifiedBy 信息。

{
  "id": 2,
  "userName": "admin",
  "manager": {
    "id": 1,
    "username": "system"
  }
}

我只是想看看这个 json。第一步必须包含 createdBy 和 lastModifiedBy。

{
  "id": 2,
  "userName": "admin",
  "manager": {
    "id": 1,
    "username": "system"
  },
  "createdBy": {
    "id": 1,
    "username": "system"
  },
  "lastModifiedBy": {
    "id": 2,
    "username": "admin"
  }
}

【问题讨论】:

    标签: java json spring-boot jackson


    【解决方案1】:

    正确的使用方法是在类级别。这些属性在JSON序列化和反序列化中被认为是被忽略的。

    示例:-

    @JsonIgnoreProperties({ "bookName", "bookCategory" })
    public class Book {
       @JsonProperty("bookId")
       private String id;
    
       @JsonProperty("bookName")        
       private String name;
    
       @JsonProperty("bookCategory")    
       private String category;  
    
    } 
    

    如果您想在字段级别忽略,可以使用@JsonIgnore

    【讨论】:

    • 它也适用于现场级别。因为“manager”字段不会导致任何错误。错误仅来自超类关系。
    • 如果要包含这些属性,为什么要使用 json ignore?
    • @Crazoute 我看不出您的代码有任何问题! @@
    • @Yasham 我只希望这些属性只有一级。我不想要更多更低的水平。因为其他级别导致stackoverflow
    【解决方案2】:

    我认为@JsonIgnoreProperties 应该放在类级别,就在你声明你的类之前。

    【讨论】:

    • 如果我声明类级别 @JsonIgnoreProperties json 即使在第一级也不包含 createdBy 和 lastModifiedBy。
    • 这不是@JsonIgnoreProperties的重点吗?
    【解决方案3】:

    @JsonIgnoreProperties(ignoreUnknown = true) 尝试在你的类名上方使用它。 当我们将 true 传递给 ignoreUnknown 元素时,在反序列化中,如果 JSON 数据有一个没有逻辑属性的字段,那么该 JSON 字段将被忽略并且不会引发错误。 像这样:

    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Book {
        @JsonProperty("bookId")
        private String id; 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      相关资源
      最近更新 更多