【问题标题】:No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer没有为类 org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor 找到序列化程序,也没有发现用于创建 BeanSerializer 的属性
【发布时间】:2019-10-03 16:40:44
【问题描述】:

我正在使用带有 Spring Data JPA 的 Spring Boot。但是,当我尝试从我的存储库中检索数据时,会引发以下错误:

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) (through reference chain: com.example.model.Pokemon$HibernateProxy$DuUnG9om[\"hibernateLazyInitializer\"])

我看到的大多数“重复”都有关系,但我的口袋妖怪课没有。有什么我遗漏的吗?

我的 Pokemon 类是一个简单的 POJO 类:

package com.example.model;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Pokemon implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -2228784815938588107L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    private String name;
    private Double attack, defense, speed;

    public Pokemon() {

    }

    public Pokemon(int id, String name, double attack, double defense, double speed) {
        super();
        this.id = id;
        this.name = name;
        this.attack = attack;
        this.defense = defense;
        this.speed = speed;
    }

    // Getters and setters
}

【问题讨论】:

  • 尝试添加getter和setter
  • @Aris_Kortex 我在最后一行注释了我的 getter 和 setter

标签: java spring hibernate spring-data-jpa


【解决方案1】:

我能够通过使用@JsonIgnoreProperties 注释解决这个问题。

// package and imports

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties({"hibernateLazyInitializer"})
@Entity
public class Pokemon implements Serializable {

// rest of code

}

【讨论】:

    【解决方案2】:

    也许尝试为您的字段添加注释,以将列名与字段映射 @Column(名称=“名称”) 私有字符串名称;

    还是你把注解放到了getter和setter上?

    【讨论】:

    • 我只是在每个字段中添加了@Column,但出现了同样的错误
    【解决方案3】:

    使用您的存储库,当您检索一个口袋妖怪时,请使用以下方法:

    findById(id).get();
    

    【讨论】:

      【解决方案4】:

      为我解决这个问题的方法是使用 jackson-datatype-hibernate5 并在 objectmapper 中注册此模块

      【讨论】:

        猜你喜欢
        • 2017-05-27
        • 1970-01-01
        • 2012-10-25
        • 2014-11-21
        • 2018-02-05
        • 2016-02-17
        • 2021-06-26
        • 2020-04-22
        • 2019-03-10
        相关资源
        最近更新 更多