クラス org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor のシリアライザーが見つかりません 質問する

クラス org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor のシリアライザーが見つかりません 質問する

エンドポイントに移動しようとすると、次のエラーが発生します

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

ベストアンサー1

Spring リポジトリのチュートリアルを実行しているときに、このエラーが発生しました。エンティティのサービス クラスを構築する段階でエラーが発生したことが判明しました。

serviceImpl クラスには、おそらく次のようなものがあります:

    @Override
    public YourEntityClass findYourEntityClassById(Long id) {
      return YourEntityClassRepositorie.getOne(id);
    }

これを次のように変更します:

    @Override
    public YourEntityClass findYourEntityClassById(Long id) {
      return YourEntityClassRepositorie.findById(id).get();
    }

基本的に、getOne は遅延ロード操作です。したがって、エンティティへの参照 (プロキシ) のみが取得されます。つまり、DB アクセスは実際には行われません。プロパティを呼び出すときにのみ、DB を照会します。findByID は、呼び出されるとすぐに呼び出しを実行するため、実際のエンティティが完全に設定されます。

これをみて:getOne と findByID の違いへのリンク

おすすめ記事