I am new to Spring MVC framework. I am trying to retrieve User details using Hibernate to return object in my Spring project. I am getting the following error:
WARN : org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: 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.ppts.mschef.util.api.ApiResponse["object"]->com.ppts.mschef.model.Mischef["user"]->com.ppts.mschef.model.User_$$_jvstb3_6["handler"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: 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.ppts.mschef.util.api.ApiResponse["object"]->com.ppts.mschef.model.Mischef["user"]->com.ppts.mschef.model.User_$$_jvstb3_6["handler"])
Can anyone tell the solution for this error??
This works for me :
http://www.geekabyte.io/2013/09/fixing-converterhttpmessagenotwritablee.html
The fix is to get Jackson to be able to handle bi-directional references. And this is done by using two Annotations: @JsonManagedReference and @JsonBackReference.
@JsonManagedReference is used to annotate the inverse side while @JsonBackReference maps the owning side of the relationship.
Example :
@Entity
class Parent {
@Id
@Column(name="parent_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private Parent wife;
@OneToMany(mappedBy="parent" cascade = CascadeType.ALL)
@JsonManagedReference
private Collection<Child> children = new ArrayList<>();
...
}
@Entity
class Child {
private String name;
@ManyToOne
@JoinColumn(name="parent_id", referencedColumn="parent_id")
@JsonBackReference
private Parent parent;
...
}
Does your user model have nested child models? Basically since all of these models are lazy loaded, you seem to run into the error mentioned above. You could initialize the child objects by a named query and pull them into the persistence context for the user object which should fix the issue.
You should declare all your relationships with the fetch type = eager --> fetch = FetchType.EAGER, but if your relationship is ignored using the annoptation @JsonIgnore is not necessary to do it.
example:
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="area_id")
@NotNull(message = "El campo area no puede ir vacío")
@JsonView(DataTablesOutput.View.class)
private Area area;
@OneToMany(mappedBy = "proceso", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonIgnore
private List<Trabajador> trabajadorList;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With