当我尝试导航到端点时出现以下错误
Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor]; nested exception is 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)
我检查了我所有的模型,所有的属性都有 getter 和 setter。所以有什么问题 ?
我可以通过添加 spring.jackson.serialization.fail-on-empty-beans=false
来解决这个问题,但我认为这只是隐藏异常的一种解决方法。
编辑
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
类:
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
类:
public class RestResponse<T> {
private String status;
private int code;
private String message;
private T result;
// Getters & Setters
}
在我的控制器中,我返回 ResponseEntity<RestResponse<PagedResponse<Product>>>
原文由 Ayoub k 发布,翻译遵循 CC BY-SA 4.0 许可协议
我在使用 spring 存储库进行教程时遇到了这个错误。原来是在为我的实体构建服务类的阶段出错了。
在你的 serviceImpl 类中,你可能有类似的东西:
将此更改为:
基本上 getOne 是一个延迟加载操作。因此,您只能获得对该实体的引用(代理)。这意味着实际上没有进行数据库访问。只有当您调用它的属性时,它才会查询数据库。 findByID 在您调用它时“急切地”/立即执行调用,因此您拥有完全填充的实际实体。
看看这个: 链接到 getOne 和 findByID 之间的区别