找不到针对类org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor的序列化器

时间:2018-10-04 23:49:38

标签: 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
}

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>>>

10 个答案:

答案 0 :(得分:5)

将FetchType从懒惰更改为热切渴望对我有用。

答案 1 :(得分:2)

@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;

答案 2 :(得分:1)

您可以通过

忽略生成属性的JSON输出
public function insert(Request $request)
{
     $id = $request->input('id'); 
     dd($request->id); 
     $firstname = $request->input('firstname'); 
     $lastname = $request->input('lastname'); 
     $mobile = $request->input('mobile');
     $data = array('id'=>$id,"firstname"=>$firstname,"lastname"=>$lastname,"mobile"=>$mobile); 
     DB::table('information')->insert($data);
}

或者如果您有任何具有关系的延迟加载属性。您可以在属性顶部使用此注释。

@JsonIgnore 

示例:

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) 

希望您的问题得到解决。谢谢。

答案 3 :(得分:1)

我在使用Spring存储库进行教程时遇到了此错误。原来,该错误是在为我的实体建立服务类的阶段犯的。

在您的serviceImpl类中,您可能会有类似的内容:

df = sorted_list_df.select('country', 'sorted_list').groupBy('coutry', 'sorted_list').agg(F.count('sorted_list'))

将此更改为:

from pyspark.sql import functions as F
from pyspark.sql import Window

w = Window.partitionBy('action').orderBy('date')

df_2 = df.withColumn("sorted_list", F.collect_set("action").over(Window.partitionBy("id").orderBy("date")))

test = df_2.select('id', 'country', 'sorted_list')\
           .dropDuplicates()\
           .select('country', 'sorted_list')\
           .groupBy('site_name', 'sorted_list')\
           .agg(F.count('sorted_list'))

这是因为getOne()返回引用。

答案 4 :(得分:1)

我也遇到了同样的问题。 我正在使用repo.getOne(id); 我将其更改为repo.findById(id)。它返回可选的,但现在错误消失了

答案 5 :(得分:1)

这解决了我的问题。

 @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})

答案 6 :(得分:0)

嗯,您是要将实体从jvm的一个实例发送到另一个需要序列化它们的实体吗?如果是这种情况,我认为错误是因为您以某种方式获取了实体,并且休眠使用其不可序列化的类,则需要将实体转换为pojo的(我的意思是使用本机类型或可序列化的对象)。

答案 7 :(得分:0)

我也面临这个问题。 @Szelek的回答帮助了我。但是我用另一种方式做到了。将getOne()方法更改为:

repository.findById(id).orElse(null)

答案 8 :(得分:0)

从MyEntityClassRepositorie.getOne(id)更改为MyEntityClassRepositorie.findById(id).get()对我来说很好

答案 9 :(得分:0)

对我来说,我收到了 DTO 对象的这个错误。问题是我没有为 DTO 属性提供 getter。因此,Jackson 无法获取这些值并假设 bean 为空。 解决方案

<块引用>

将 Getter 添加到您的 DTO

相关问题