[英]Spring Data JPA, error while sending data to postman from Rest Controller, Nested exception
I got this error when I was trying to send data, in form of a List Object from Rest Controller to postman.我在尝试发送数据时收到此错误,以列表 Object 的形式从 Rest Controller 到 Z051104762CB860AFFCCFA I know what caused this problem but don't know how to solve it?
我知道是什么导致了这个问题,但不知道如何解决? code for the Rest Entity and Controller is as follows:
Rest 实体和 Controller 的代码如下:
@Entity
@Table(name = "posts")
public class PostEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@OneToMany(mappedBy = "postEntity")
private List<CommentEntity> comments;
// getters and setters below
}
@Entity
@Table(name = "comments")
public class CommentEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE,CascadeType.DETACH, CascadeType.REFRESH})
@JoinColumn(name = "post_id")
private PostEntity postEntity;
// getters and setters
}
public List<PostEntity> getFilteredData() {
List<PostEntity> posts = postService.findAll();
return posts;
}
Error Message:错误信息:
2021-02-11 20:03:57.417 ERROR 26199 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]
: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotWritableException:
Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError)
(through reference chain:
com.akashmjain.BlogApplication.enitity.UserEntity["posts"]->org.hibernate.collection.internal.Pe
rsistentBag[0]->com.akashmjain.BlogApplication.enitity.PostEntity["author"]->com.akashmjain.Blog
Application.enitity.UserEntity["posts"]->org.hibernate.collection.internal.PersistentBag[0]->com
.akashmjain.BlogApplication.enitity.PostEntity["author"]->com.akashmjain.BlogApplication.enitity.UserEntity["posts"]->org.hibernate.collection.internal.PersistentBag[0]->com.akashmjain.BlogApp
lication.enitity.PostEntity["author"]->
com.akashmjain.BlogApplication.enitity.UserEntity["posts"]->org.hibernate.collection.internal.PersistentBag[0]->com.akashmjain.BlogApplication.enitity.Po
stEntity["author"]->com.akashmjain.BlogApplication.enitity.UserEntity["posts"]->org.hibernate.collection.internal.PersistentBag[0]->com.akashmjain.BlogApplication.enitity.PostEntity["author"]-
>com.akashmjain.BlogApplication.enitity.UserEntity["posts"]->org.hibernate.collection.internal.PersistentBag[0]->com.akashmjain.BlogApplication.enitity.PostEntity["author"]-
Here I think the problem is The Comment and Post Entity calling each other getters to send the data to the rest controller.在这里我认为问题是评论和发布实体相互调用getter以将数据发送到rest controller。 So I tried changing the toString() to only have Primitive types and wrapper classes.
所以我尝试将 toString() 更改为只有原始类型和包装类。 but still, there is the same error.
但是,仍然存在相同的错误。 Am I write on this or is there something I am missing?
我是在写这个还是有什么我遗漏的?
You have a unidirectional relation between the two entities which makes Jackson fail to serialize/deserialize the payload due to the infinite recursion.您在两个实体之间存在单向关系,这使得 Jackson 由于无限递归而无法序列化/反序列化有效负载。
You can try the following that will hopefully resolve the issue:您可以尝试以下方法,希望能解决问题:
@OneToMany(mappedBy = "postEntity")
@JsonBackReference
private List<CommentEntity> comments;
Annotate the list of comments using @JsonBackReference
and then do the following on the CommentEntity
but this time use the following annotation:使用
@JsonBackReference
注释评论列表,然后在CommentEntity
上执行以下操作,但这次使用以下注释:
@JoinColumn(name = "post_id")
@JsonManagedReference
private PostEntity postEntity;
You can check on more on that here https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion您可以在此处查看更多信息https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.