com.fasterxml.jackson.databind.exc.InvalidDefinitionException: 找不到类org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor的序列化程序
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: 找不到类org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor的序列化程序
我已经浏览过:Could not write JSON: Infinite recursion (StackOverflowError); nested exception spring boot 和类似的链接。我正在使用 Spring Boot 2.1.6.RELEASE and Data JPA and Postgres
的例子。\n这是我的源代码:https://github.com/JavaHelper/issue-jackson-boot\n错误信息:\n
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.data.domain.PageImpl["content"]->java.util.Collections$UnmodifiableRandomAccessList[0]->com.example.demo.entity.StockDailyRecord["stock"]->com.example.demo.entity.Stock$HibernateProxy$f8byIJ39["hibernateLazyInitializer"]) at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.9.9.jar:2.9.9] at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191) ~[jackson-databind-2.9.9.jar:2.9.9] ...
\nStock.java\n
@Data @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode(callSuper = true) @Entity @Table(name = "stock", uniqueConstraints = { @UniqueConstraint(columnNames = "STOCK_NAME"), @UniqueConstraint(columnNames = "STOCK_CODE") }) public class Stock extends BaseEntity implements java.io.Serializable { ... }
\nStockDailyRecord.java\n
@Data @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode(callSuper = true) @Entity @Table(name = "stock_daily_record") public class StockDailyRecord extends BaseEntity implements java.io.Serializable { ... }
\nBaseEntity.java\n
@Data @NoArgsConstructor @AllArgsConstructor @MappedSuperclass @EntityListeners(AuditingEntityListener.class) public class BaseEntity { ... }
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor
问题原因:根据给出的代码和相关链接的内容来看,这个问题是由于使用了Jackson库来序列化对象时,Jackson无法找到org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor类的序列化器所导致的。这个类是Hibernate框架在处理懒加载关联时使用的代理类。
解决方法:根据提供的链接和代码内容,可以采取以下解决方法:
1. 在Stock类中添加以下注解,排除掉不需要序列化的属性:
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "stockId") @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
2. 在相关的getter方法上添加@JsonIgnore注解,排除掉不需要序列化的属性:
@JsonIgnore public SetgetStockDailyRecords() { return stockDailyRecords; }
3. 可以考虑使用Jackson的@JsonManagedReference和@JsonBackReference注解来解决双向关联的序列化问题。具体使用方法可以参考给出的链接中的示例。
以上是关于com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor问题的解决方法。采用上述方法可以解决Jackson在处理Hibernate的懒加载关联时遇到的序列化问题。