Object Serialization Error with Spring Data and Spring Web

Asked

Viewed 242 times

2

I was creating a simple endpoint of an entity and ended up getting the following exception:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.logpro.appcidadao.model.imovel.Imovel_$$_jvst35_0["handler"])

With some research, I came up with a solution to the problem. I added the configuration to my application.properites (jackson.erialization.FAIL_ON_EMPTY_BEANS=false) not to serialize empty properties and noted my class as follows:

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

With this everything worked correctly. However, that’s where my doubt comes in. My class Imovelhas only one attribute, which is id. How you are triggering the lazyInitiliazer error if the class no longer has any attribute?

Immovable Class:

@Entity
@Table(schema = "cadastro", name = "imovel")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
//@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Imovel {

   @Id
   @Column(name = "imov_id")
   private Integer id;

}
  1. Spring boot: 2.0.3
  2. Spring: 5.0.7

1 answer

2


So the problem was in my repository. I have a class called ImovelRepository extending JpaRepository. In an earlier version, I used the method findOne passing the ID of the object I wanted to load. It turns out that in the new API, the method findOne no longer receives an ID, and yes, receives a Example. With that, I changed the findOne for getOne, thinking would do the same thing. However, the getOne works in a similar way to getReference, leaving the class in Azy mode. With that, I went to look at the documentation of spring data 5, and saw that the method that returns an object searching by the ID is the findById (new name). Switching to this method, it worked correctly as expected.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.