无法写入HTTP消息:org.springframework.http.converter.HttpMessageNotWritableException:

Ish*_*Ish 4 java spring hibernate spring-mvc

我是Spring MVC框架的新手。我正在尝试使用Hibernate检索User详细信息以返回Spring项目中的对象。我收到以下错误:

WARN:org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver-无法写入HTTP消息:org.springframework.http.converter.HttpMessageNotWritableException:无法写入内容:没有为org.hibernate.proxy.pojo类找到序列化程序。 javassist.JavassistLazyInitializer,未发现创建BeanSerializer的属性(为避免异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS))(通过参考链:com.ppts.mschef.util.api.ApiResponse [“ object”]-> com.ppts.mschef。) model.Mischef [“ user”]-> com.ppts.mschef.model.User _ $$ _ jvstb3_6 [“ handler”]); 嵌套的异常是com.fasterxml.jackson.databind.JsonMappingException:没有为org.hibernate.proxy.pojo.javassist类找到序列化程序。

谁能告诉这个错误的解决方案?

Dev*_*aul 8

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;
...
}
Run Code Online (Sandbox Code Playgroud)


小智 1

您的用户模型是否有嵌套的子模型?基本上,由于所有这些模型都是延迟加载的,因此您似乎遇到了上述错误。您可以通过命名查询初始化子对象,并将它们拉入用户对象的持久性上下文中,这应该可以解决问题。