简体   繁体   English

Spring Data JPA, error while sending data to postman from Rest Controller, Nested exception

[英]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.

相关问题 通过REST控制器使用Spring Data JPA和QueryDsl的异常 - Exception using Spring Data JPA and QueryDsl via REST Controller 从Jsp向Rest Controller发送Json数据时获取415(不支持的媒体类型错误) - Getting 415(Unsupported media type error) while sending the Json data from Jsp to Rest Controller Spring Data REST JPA:将自动CRUD操作与手动控制器集成在一起 - Spring Data REST JPA: Integrate automatic CRUD operations with manual controller 带有REST日志的Spring JPA数据 - Spring JPA Data with REST Log Spring Data Jpa超时异常 - Timeout Exception with Spring Data Jpa 春季数据:JPA和嵌套事务 - Spring Data: JPA and Nested Transaction Spring Boot Api 在邮递员中工作正常,用于印地文文本,但从移动应用程序发送请求时印地文数据未正确接收 - Spring boot Api working fine in postman for hindi text but while sending request from mobile apps hindi data not receiving properly 将Json数据发送到Spring Controller - Sending Json Data to Spring Controller Spring Data REST + JPA 从 OneToMany 集合中删除 [不是所有者方面] - Spring Data REST + JPA remove from OneToMany collection [not owner side] Spring Rest Template创建多部分表单/数据客户端,就像邮递员一样抛出不可转换的异常 - Spring Rest Template creating multipart form/data client working like a postman throws non convertable exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM