【问题标题】:Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException:无法写入 HTTP 消息:org.springframework.http.converter.HttpMessageNotWritableException:
【发布时间】:2016-08-27 06:20:04
【问题描述】:

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

警告: 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.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" ])

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

【问题讨论】:

标签: java spring hibernate spring-mvc


【解决方案1】:
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;
...
}

【讨论】:

  • 我遇到了完全相同的异常,但在我的情况下,我只有一个实体 Student 具有 rollNo(PRIMARY KEY)、姓名、性别、年级.....你知道什么吗在那种情况下怎么办?
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    您应该使用 fetch type = eager --> fetch = FetchType.EAGER 声明您的所有关系,但如果您的关系被忽略,则无需使用注解 @JsonIgnore。

    示例:

    @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;
    

    【讨论】:

      猜你喜欢
      • 2018-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 2018-09-19
      • 2017-05-26
      • 2018-09-08
      • 1970-01-01
      相关资源
      最近更新 更多