当我尝试导航到端点时,出现以下错误
类型定义错误:[简单类型,类org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor];嵌套的异常是com.fasterxml.jackson.databind.exc.InvalidDefinitionException:未为类org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor找到序列化程序,也未找到创建BeanSerializer的属性(为避免异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)
我检查了所有模型,所有属性都有getter和setter。所以有什么问题 ?
我可以通过添加来解决此问题,spring.jackson.serialization.fail-on-empty- beans=false但我认为这只是隐藏异常的一种解决方法。
spring.jackson.serialization.fail-on-empty- beans=false
编辑
Product 模型:
Product
@Entity public class Product { private int id; private String name; private String photo; private double price; private int quantity; private Double rating; private Provider provider; private String description; private List<Category> categories = new ArrayList<>(); private List<Photo> photos = new ArrayList<>(); // Getters & Setters }
PagedResponse 类:
PagedResponse
public class PagedResponse<T> { private List<T> content; private int page; private int size; private long totalElements; private int totalPages; private boolean last; // Getters & Setters }
RestResponse 类:
RestResponse
public class RestResponse<T> { private String status; private int code; private String message; private T result; // Getters & Setters }
在我的控制器中,我要返回 ResponseEntity<RestResponse<PagedResponse<Product>>>
ResponseEntity<RestResponse<PagedResponse<Product>>>
您可以通过以下方式忽略生成属性的JSON输出:
@JsonIgnore
或者如果您有任何具有关系的延迟加载属性。您可以在属性顶部使用此注释。
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
例:
@Entity public class Product implements Serializable{ private int id; private String name; private String photo; private double price; private int quantity; private Double rating; private Provider provider; private String description; @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) private List<Category> categories = new ArrayList<>(); @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) private List<Photo> photos = new ArrayList<>(); // Getters & Setters }
如果仍然有此错误,请在您的application.properties文件中添加以下代码行
spring.jackson.serialization.fail-on-empty-beans=false
希望您的问题能得到解决。谢谢。