Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException:

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

like image 673
Ish Avatar asked May 02 '16 13:05

Ish


3 Answers

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;
...
}
like image 118
Devendra Singraul Avatar answered Oct 18 '22 15:10

Devendra Singraul


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.

like image 20
svalivarthi Avatar answered Oct 18 '22 14:10

svalivarthi


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;
like image 30
Jason Esteven Lazo Lock Avatar answered Oct 18 '22 13:10

Jason Esteven Lazo Lock