【问题标题】:How to post ManyToOne and OneToOne entity using JSON format?如何使用 JSON 格式发布 ManyToOne 和 OneToOne 实体?
【发布时间】:2020-02-10 21:57:54
【问题描述】:

我有 comment 实体,它有 user_idproject_id 作为外键。并且 cmets 可以在parent_commnent_id 提到的同一个表中具有父子关系。

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table
public class Comment {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "comment_id")
    private int commentId;

    @ManyToOne
    @JsonIgnore
    @JoinColumn(name = "project_id")
    private Project projectId;

    @Column(name = "comment")
    private String comment;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id", referencedColumnName = "user_id")

    private RegisteredUser user;

    @ManyToOne
    @JsonIgnore
    @JoinColumn(name = "Parent_comment_id")
    public Comment ParentCommentId;

    @OneToMany(mappedBy = "ParentCommentId")
    public List<Comment> childComments = new ArrayList<Comment>();

    public int getCommentId() {
        return commentId;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public Project getProjectId() {
        return projectId;
    }

    public void setProjectId(Project projectId) {
        this.projectId = projectId;
    }

    public RegisteredUser getUser() {
        return user;
    }

    public void setUser(RegisteredUser user) {
        this.user = user;
    }

    public String getComment() {
        return comment;
    }

    public void setCommentId(int commentId) {
        this.commentId = commentId;
    }
}

我手动插入了一些数据

INSERT INTO comment (comment, Parent_comment_id, project_id, user_id) VALUES ( 'first comment', null, 1, 1);
INSERT INTO comment (comment, Parent_comment_id, project_id, user_id) VALUES ( 'first child comment', 1, 1, 2);

这让我回了这个

但我无法通过邮递员发布数据。这个我试过了

{
    "comment": "third comment",
    "userId": 2, 
    "projectId" : 3,
    "parentCommentId" : 2
}

插入数据但将userIdprojectIdparentCommentId作为null返回。

我也试过这个。

{    
     "comment": "third comment",
     "user": {
            "userId" : 2
        }
}

这给了我 500 错误。

【问题讨论】:

  • 你写过任何控制器类和服务类来返回这个吗,接下来只传递一个唯一的 id 以便它为你提供例外的数据
  • 是的,在控制器中我将评论对象传递给存储库的保存功能。

标签: java spring-boot gradle spring-data-jpa


【解决方案1】:

我认为您可以简单地发送userId,然后根据userIduser 表(您从用户那里提到的任何名称)获取详细信息。

您为什么要在帖子中发送完整的对象,或者您可以使用@Embedded 注释。

希望它会起作用。

【讨论】:

    【解决方案2】:

    按照您的做法,Spring 期望您的 JSON 是这样的:

    {
        "comment": "Foo bar",
        "projectId": {
            "id": 123,
            ..other properties..
        },
        "user": {
            "id": 1
            ..other properties..
        },
        "parentCommentId": {
            "id": 456
            ..other properties..
        },
        "childComments": [
            {   
                "id": 789
                ..other properties..
            }
        ]
    }
    

    另一方面,你是这样插入的:

    {
        "comment": "third comment",
        "userId": 2, 
        "projectId" : 3,
        "parentCommentId" : 2
    }
    

    您明确定义了RegisteredUserProjectComment。 Spring 并不真正知道 JSON 中的属性 userIdRegisteredUser 对象起作用。

    我试过了

    {
        "comment": "third comment",
        "userId": 2, 
        "projectId" : 3,
        "parentCommentId" : 2
    }
    

    插入数据但返回 userId、projectId 和 parentCommentId 为 空。

    为什么会这样? 好吧,您的 Comment 类没有定义 userIdprojectIdparentCommentId 属性。

    这是反序列化程序的预期行为。如果您不想手动创建对象(即:使用 DTO),则必须告诉反序列化器创建 RegisteredUserProjectComment 的实例(用于 parentComment)。

    为此,您只需在 JSON 中创建对象。

    而不是这个:

    {
        "comment": "third comment",
        "userId": 2, 
        "projectId" : 3,
        "parentCommentId" : 2
    }
    

    试试这个

    {
        "comment": "Foo bar",
        "projectId": {
            "projectId": 3
        },
        "user": {
            "userId": 1
        },
        "parentCommentId": {
            "commentId": 2
        }
    }
    

    这可行,但比您尝试插入的方式要冗长得多。这就是我总是将 DTO 用于请求/响应对象的原因之一。

    【讨论】:

      猜你喜欢
      • 2019-12-05
      • 1970-01-01
      • 2023-03-28
      • 2015-03-27
      • 1970-01-01
      • 2017-07-15
      • 2020-04-10
      • 1970-01-01
      相关资源
      最近更新 更多