77
votes

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

10
I faced the same exact issue, added the prop entry and I'm able to see the response, previously it was failing. Thanks for this questions and the hint fail-on-empty-beansAnand Rockzz
check the answer here stackoverflow.com/a/51129161/2160969Wiz

10 Answers

108
votes

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

87
votes

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.

10
votes

Changing the FetchType from lazy to eager did the trick for me.

5
votes

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) work for me very well. It doesn't miss any reference objects and resolve the problem.

In my case:

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

i also faced same problem. I was using repo.getOne(id); i changed it to repo.findById(id). It returned optional, but now error is gone

2
votes

I also faced with this problem. @Szelek's answer helped me. But I did it with another way. Changed getOne() method to:

repository.findById(id).orElse(null)

Ensure you are taking care of the NullPointerException this will generate when it's not found.

2
votes

This solved my issue.

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

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

0
votes

For me, I got this error for a DTO object. The problem was I didn't provide getters for DTO properties. Therefore, Jackson was not able to fetch those values and assumed the bean is empty. Solution:

Add Getters to your DTO

0
votes

Changing from

MyEntityClassRepositorie.getOne(id)

to

MyEntityClassRepositorie.findById(id).get()

work fine for me.