当我尝试导航到端点时,出现以下错误
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)
我检查了我所有的模型,所有属性都有吸气剂和二传手。那么问题出在哪里呢?
我可以通过添加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>>>
我在使用 spring 存储库进行教程时遇到了此错误。事实证明,错误是在为我的实体构建服务类的阶段犯的。
在你的 serviceImpl 类中,你可能有这样的东西:
@Override
public YourEntityClass findYourEntityClassById(Long id) {
return YourEntityClassRepositorie.getOne(id);
}
将其更改为:
@Override
public YourEntityClass findYourEntityClassById(Long id) {
return YourEntityClassRepositorie.findById(id).get();
}
基本上getOne是一个延迟加载操作。因此,您只能获得对实体的引用(代理)。这意味着实际上没有进行数据库访问。只有当你调用它的属性时,它才会查询数据库。findByID 在您调用它时"急切地"/立即进行调用,因此您已完全填充实际实体。
看看这个:链接到getOne和findByID之间的区别
您可以忽略以通过以下方式生成属性的 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
我希望你的问题能得到解决。谢谢。
将FetchType
从Lazy
更改为Eager
对我来说是诀窍。
这个答案来自书中:"使用 Spring boot 学习微服务" 与其抑制弹簧建议的空豆上的错误,不如有更可取的方法来处理这个问题。
我们将嵌套的用户实体配置为在 LAZY 模式下获取,因此 不会从数据库中查询它们。我们还说过 Hibernate在运行时为我们的类创建代理。那就是 ByteBuddyInterceptor 类背后的原因。您可以尝试切换 获取模式到 EAGER,您将不再收到此错误。但 这不是这个问题的正确解决方案,因为那时我们将 触发许多对我们不需要的数据的查询。让我们保持懒惰 获取模式并相应地修复此问题。我们的第一个选择是 自定义我们的 JSON 序列化,以便它可以处理 Hibernate 对象。 幸运的是,Franc逊库的提供商FasterXML有一个特定的 我们可以在 ObjectMapper 对象中使用的休眠模块: Jackson-datatype-hibernate:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5</artifactId>
</dependency>
我们为杰克逊的新Hibernate模块创建了一个bean。春天 Boot的Jackson2ObjectMapperBuilder将通过自动配置使用它, 我们所有的 ObjectMapper 实例都将使用 Spring Boot 默认值。 加上我们自己的定制。
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JsonConfiguration {
@Bean
public Module hibernateModule() {
return new Hibernate5Module();
}
}
@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;
这解决了我的问题。
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
我也遇到了同样的问题。 我正在使用repo.getOne(id); 我把它改成了repo.findById(id)。它返回可选,但现在错误消失了
更改自
MyEntityClassRepositorie.getOne(id)
自
MyEntityClassRepositorie.findById(id).get()
对我来说工作正常。
我也面临这个问题。 @Szelek的回答帮助了我。但我用另一种方式做到了。将 getOne() 方法更改为:
repository.findById(id).orElse(null)
确保您正在处理未找到时将生成的NullPointerException
。
在 pom 中添加以下依赖项.xml for javax。
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5</artifactId>
或跟随雅加达。
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5-jakarta</artifactId>
并为 javax.* 添加如下配置:
package microservices.book.multiplication.configuration;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.datatype.hibernate5.jakarta.Hibernate5JakartaModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JsonConfiguration {
@Bean
public Module hibernateModule() {
return new Hibernate5JakartaModule();
}
}
或者对于雅加达.*软件包,添加配置如下:
package microservices.book.multiplication.configuration;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.datatype.hibernate5.jakarta.Hibernate5JakartaModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JsonConfiguration {
@Bean
public Module hibernateModule() {
return new Hibernate5JakartaModule();
}
}
以上两个步骤应该可以解决此问题。
嗯,您是否正在将实体从 jvm 的一个实例发送到另一个需要序列化它们的实例? 如果是这种情况,我认为错误是因为您以某种方式获取了实体并且 Hibernate 正在使用其不可序列化的类,您需要将实体转换为 pojo(我的意思是使用本机类型或可序列化的对象)。
对我来说,我收到了DTO对象的此错误。问题是我没有为 DTO 属性提供吸气剂。因此,杰克逊无法获取这些值,并假设 bean 为空。解决方案:
将 Getters 添加到您的 DTO
就我而言,我面临着与@EntityGraph
相同的异常。实际上,我试图通过EntityGraph获取OneToMany(库存列表)并且它正在工作,但是当我尝试与ManyToOne(类别)建立另一个关系时,它给出了错误。我不知道为什么它变得懒惰。众所周知,@ManyToOne
默认是渴望的。
class Product {
@ManyToOne
@JoinColumn(name = "categoryId")
private Category category;
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL)
private List<Inventory> inventoryList;
}
存储库方法:
@EntityGraph(attributePaths = { "inventoryList" })
List<Product> findAll();
我得到了同样的期望,因为我没有在实体图提取中添加类别。添加类别后,它得到了修复。
@EntityGraph(attributePaths = { "inventoryList", "category" })
List<Product> findAll();
一旦我转向使用jakarta.persistence
库,我就遇到了这个问题,一旦我改回javax.persistence
问题就解决了。
如果您使用的是 spring 6,那么它将起作用
spring:
jackson:
serialization:
fail-on-empty-beans: false
<</div> div class="one_answers">我从其他文章中找到了方法,您可以在类中添加公共 setter 和 getter,它可以解决这个问题
当我在本地主机上工作时,我只需要一个服务器。以下内容对我有用
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.65</version>
</dependency>