Java 找不到类 org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor 的序列化程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52656517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor
提问by Ayoub k
When i try to navigat to an endpoint i get tho following error
当我尝试导航到端点时,出现以下错误
Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor]; nested exception is com.fasterxml.Hymanson.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)
类型定义错误:[简单类型,类org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor];嵌套异常是 com.fasterxml.Hymanson.databind.exc.InvalidDefinitionException:未找到类 org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor 的序列化程序,也未发现用于创建 BeanSerializer 的属性(为避免异常,请禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS)
I checked all my models and all the attributes have getters and setters. So what's the problem ?
我检查了我所有的模型,所有的属性都有 getter 和 setter。所以有什么问题 ?
I can fix that by adding spring.Hymanson.serialization.fail-on-empty-beans=false
but i think this is just a work around to hide the exception.
我可以通过添加来解决这个问题,spring.Hymanson.serialization.fail-on-empty-beans=false
但我认为这只是一种隐藏异常的解决方法。
Edit
编辑
Product
model:
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
class :
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
Class :
RestResponse
班级 :
public class RestResponse<T> {
private String status;
private int code;
private String message;
private T result;
// Getters & Setters
}
In my controller i'm returning ResponseEntity<RestResponse<PagedResponse<Product>>>
在我的控制器中,我回来了 ResponseEntity<RestResponse<PagedResponse<Product>>>
采纳答案by Subarata Talukder
You can Ignore to produce JSON output of a property by
您可以忽略以通过以下方式生成属性的 JSON 输出
@JsonIgnore
Or If you have any lazy loaded properties having a relationship. You can use this annotation at top of the property.
或者,如果您有任何延迟加载的属性有关系。您可以在属性顶部使用此注释。
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
Example:
例子:
@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
}
If you still have this error, please add this line of code in your application.properties file
如果你仍然有这个错误,请在你的 application.properties 文件中添加这行代码
spring.Hymanson.serialization.fail-on-empty-beans=false
I hope your problem will be solved. Thanks.
我希望你的问题会得到解决。谢谢。
回答by Ivan Perales M.
Hmm are you traying to send entities from one instance of the jvm to another one which need to serialize them? if this is the case i think the error is because you fetched the entities somehow and hibernate is using its not serializable classes, you need to convert entities to pojo's (i mean use native types or objects that are serializables).
嗯,您是否正在尝试将实体从 jvm 的一个实例发送到另一个需要序列化它们的实例?如果是这种情况,我认为错误是因为您以某种方式获取了实体并且休眠正在使用其不可序列化的类,您需要将实体转换为 pojo 的(我的意思是使用可序列化的本机类型或对象)。
回答by Szelek
I came across this error while doing a tutorial with spring repository. It turned out that the error was made at the stage of building the service class for my entity.
我在使用 spring 存储库做教程时遇到了这个错误。原来错误是在为我的实体构建服务类的阶段发生的。
In your serviceImpl class, you probably have something like:
在您的 serviceImpl 类中,您可能有以下内容:
@Override
public YourEntityClass findYourEntityClassById(Long id) {
return YourEntityClassRepositorie.getOne(id);
}
Change this to:
将此更改为:
@Override
public YourEntityClass findYourEntityClassById(Long id) {
return YourEntityClassRepositorie.findById(id).get();
}
It's because the getOne(), returns a reference.
这是因为 getOne() 返回一个引用。
回答by SNabi
I also faced with this problem. @Szelek's answer helped me. But I did it with another way. Changed getOne() method to:
我也遇到了这个问题。@Szelek 的回答帮助了我。但我用另一种方式做到了。将 getOne() 方法更改为:
repository.findById(id).orElse(null)
回答by Chris Neve
Changing the FetchType from lazy to eager did the trick for me.
将 FetchType 从懒惰更改为渴望对我来说很有效。