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

标签 java spring hibernate spring-mvc

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

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"])

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

最佳答案

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;
...
}

关于java - 无法写入 HTTP 消息 : org. springframework.http.converter.HttpMessageNotWritableException:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36983215/

相关文章:

java - 用于返回正在进行的拍卖的 SQL 查询

java - 使用 AbstractTableModel 将行添加到 JTable

java - 如何对给定的字符串模式使用 Java 正则表达式或 Java 流并从中创建映射

spring - 找不到Elasticsearch Spring JSONObject [“hits”]

java - 基本类型的一对一映射,到 Hibernate 中的单个类

java - 如何通过Spring Boot API获取特定字段

java - 获取项目中的所有注释?

spring - SessionStatus object.setComplete() 是否清除所有 session 属性或仅适用于使用它的 Controller ?

java - keyHolder.getKey() 返回 null

java - 使用 MySQL 的 DataJpaTest 存储库测试,Hibernate 不会将用户设置为在 H2 DB 中自动递增