有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用JSON格式发布多通和单通实体?

我有comment实体,它将user_idproject_id作为外键。和注释可以在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);

这给了我这个

enter image description here

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

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

它插入数据,但将userIdprojectIdparentCommentId返回为null

我也试过这个

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

这给了我500个错误


共 (1) 个答案

  1. # 1 楼答案

    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对象的行为相同

    I have tried this

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

    Which insert data but return userId, projectId, and parentCommentId as null.

    为什么会这样? 你的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的原因之一