Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor

When i try to navigate to an endpoint i get the following error

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)

I checked all my models and all the attributes have getters and setters. So what's the problem ?

I can fix that by adding spring.jackson.serialization.fail-on-empty-beans=false but i think this is just a work around to hide the exception.

Edit

Product model:

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

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 :

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

like image 537
Ayoub k Avatar asked Oct 04 '18 23:10

Ayoub k


Video Answer


2 Answers

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.

In your serviceImpl class, you probably have something like:

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

Change this to:

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

Basically getOne is a lazy load operation. Thus you get only a reference (a proxy) to the entity. That means no DB access is actually made. Only when you call it's properties then it will query the DB. findByID does the call 'eagerly'/immediately when you call it, thus you have the actual entity fully populated.

Take a look at this: Link to the difference between getOne & findByID

like image 153
Szelek Avatar answered Sep 18 '22 09:09

Szelek


You can Ignore to produce JSON output of a property by

@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

spring.jackson.serialization.fail-on-empty-beans=false 

I hope your problem will be solved. Thanks.

like image 25
Subarata Talukder Avatar answered Sep 21 '22 09:09

Subarata Talukder