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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36983215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:04:26  来源:igfitidea点击:

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

javaspringhibernatespring-mvc

提问by Ish

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:

我是 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.Hymanson.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"])

警告: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.Hymanson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist。

Can anyone tell the solution for this error??

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

回答by Devendra Singraul

This works for me : 
http://www.geekabyte.io/2013/09/fixing-converterhttpmessagenotwritablee.html


The fix is to get Hymanson 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;
...
}

回答by svalivarthi

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.

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

回答by Jason Esteven Lazo Lock

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.

您应该使用 fetch type =eager --> fetch = FetchType.EAGER 声明您的所有关系,但是如果使用 annotation 忽略您的关系 @JsonIgnore 则没有必要这样做。

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;