Ayo*_*b k 13 java spring hibernate spring-mvc spring-boot
当我尝试导航到端点时,出现以下错误
类型定义错误:[简单类型,类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但我认为这只是隐藏异常的一种解决方法。
编辑
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
}
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
RestResponse 类:
public class RestResponse<T> {
private String status;
private int code;
private String message;
private T result;
// Getters & Setters
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我要返回 ResponseEntity<RestResponse<PagedResponse<Product>>>
Sze*_*lek 119
我在使用 spring 存储库做教程时遇到了这个错误。原来错误是在为我的实体构建服务类的阶段发生的。
在您的 serviceImpl 类中,您可能有以下内容:
@Override
public YourEntityClass findYourEntityClassById(Long id) {
return YourEntityClassRepositorie.getOne(id);
}
Run Code Online (Sandbox Code Playgroud)
将此更改为:
@Override
public YourEntityClass findYourEntityClassById(Long id) {
return YourEntityClassRepositorie.findById(id).get();
}
Run Code Online (Sandbox Code Playgroud)
基本上 getOne 是一个延迟加载操作。因此,您只能获得对实体的引用(代理)。这意味着实际上没有进行数据库访问。只有当你调用它的属性时,它才会查询数据库。findByID 在您调用它时会“急切地”/立即调用它,因此您已经完全填充了实际的实体。
看看这个:链接到 getOne 和 findByID 之间的区别
小智 16
这个答案来自书:“learn microservices with spring boot”\n不是抑制 spring 建议的空 bean 上的错误,而是有更好的方法来处理这个问题。
\n\n\n我们将嵌套的 User 实体配置为以 LAZY 模式获取,因此\n它们\xe2\x80\x99 不会从数据库中查询。我们还说过,\nHibernate 在运行时为我们的类创建代理。这就是 ByteBuddyInterceptor 类背后的原因。您可以尝试将获取模式切换为 EAGER,这样就不会再出现此错误。但是\n\nxe2\x80\x99s 不是这个问题的正确解决方案,从那以后我们\xe2\x80\x99 将\n触发许多我们不需要\xe2\x80\x99 不需要的数据的查询。让\xe2\x80\x99s 保持惰性\nfetch 模式并相应地修复此问题。我们的第一个选择是自定义 JSON 序列化,以便它可以处理 Hibernate 对象。幸运的是,FasterXML(Jackson 库的提供者)有一个特定的 Hibernate 模块,我们可以在 ObjectMapper 对象中使用它:jackson-datatype -休眠:
\n
<dependency>\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0<groupId>com.fasterxml.jackson.datatype</groupId>\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0<artifactId>jackson-datatype-hibernate5</artifactId>\n</dependency>\nRun Code Online (Sandbox Code Playgroud)\n\n\n我们为 Jackson 的新 Hibernate 模块创建一个 bean。Spring\nBoot\xe2\x80\x99s Jackson2ObjectMapperBuilder 将通过自动配置使用它,\n并且我们所有的 ObjectMapper 实例都将使用 Spring Boot 默认值\n加上我们自己的自定义。
\n
import com.fasterxml.jackson.databind.Module;\nimport com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\n@Configuration\npublic class JsonConfiguration {\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0@Bean\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0public Module hibernateModule() {\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0return new Hibernate5Module();\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0}\n}\nRun Code Online (Sandbox Code Playgroud)\n
Chr*_*eve 10
将 FetchType 从懒惰更改为渴望对我来说很有效。
您可以通过以下方式忽略生成属性的JSON输出:
@JsonIgnore
Run Code Online (Sandbox Code Playgroud)
或者如果您有任何具有关系的延迟加载属性。您可以在属性顶部使用此注释。
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
Run Code Online (Sandbox Code Playgroud)
例:
@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
}
Run Code Online (Sandbox Code Playgroud)
如果仍然存在此错误,请在您的application.properties文件中添加以下代码行
spring.jackson.serialization.fail-on-empty-beans=false
Run Code Online (Sandbox Code Playgroud)
希望您的问题能得到解决。谢谢。
小智 7
这解决了我的问题。
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
Run Code Online (Sandbox Code Playgroud)
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})为我工作得很好。它不会遗漏任何参考对象并解决问题。
就我而言:
@Entity
@Table(name = "applications")
public class Application implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotBlank
@Size(max = 36, min = 36)
private String guid;
@NotBlank
@Size(max = 60)
private String name;
@Column(name = "refresh_delay")
private int refreshDelay;
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "id_production", referencedColumnName = "id")
@JsonIgnoreProperties(value = {"applications", "hibernateLazyInitializer"})
private Production production;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10647 次 |
| 最近记录: |