我是 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/