如何使用JSON格式发布ManyToOne实体

时间:2019-07-27 21:48:15

标签: json spring-boot jpa

我有两个实体issueuser,我想为用户分配一个问题,但是当尝试使用JSON中的user_id issues表是NULL

@Data
@Entity
@Table(name = "issues")
public class Issue {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column
    private String number;
    @Column
    private String title;
    @Column
    private String description;
    @Column
    @Enumerated(EnumType.STRING)
    private State state;
    @JsonIgnore
    @CreationTimestamp
    @Column
    private Timestamp createDate;
    @JsonIgnore
    @UpdateTimestamp
    @Column
    private Timestamp modifyDate;
    @ManyToOne(targetEntity = User.class)
    @JoinColumn
    private User user;

    public Issue() {

    }
@Data
@Entity
@Table(name = "users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column
    private String fullName;
    @Column
    private String username;
    @Column
    private String email;
    @Column
    private String password;
    @Column
    @Enumerated(EnumType.STRING)
    private Role role;
    @JsonIgnore
    @CreationTimestamp
    @Column
    private Timestamp createDate;
    @JsonIgnore
    @UpdateTimestamp
    @Column
    private Timestamp modifyDate;

    public User() {

    }

    public User(String fullName, String username, String email, String password, Role role) {
        this.fullName = fullName;
        this.username = username;
        this.email = email;
        this.password = password;
        this.role = role;
    }

首先,我创建了一个没有任何问题的用户,但是不知道如何解决该问题,这是我通过邮递员使用的JSON。

{
    "number": "3",
    "title": "Create an working service",
    "description": "The problem is that we do not have an working service.",
    "state": "NEW",
    "user_id": "1"
}

这部分负责保存问题。

    public void save(Issue issue) {
        if (issue == null)
            return;
        Issue actual = issueRepository.findByNumber(issue.getNumber());
        if (actual != null) {
            actual.setNumber(issue.getNumber());
            actual.setTitle(issue.getTitle());
            actual.setDescription(issue.getDescription());
            actual.setState(issue.getState());
            actual.setUser(issue.getUser());
            issueRepository.save(actual);
        } else {
            issueRepository.save(issue);
        }
    }

在控制器中,我只有@Valid @RequestBody Issue问题,该服务保存了问题。

3 个答案:

答案 0 :(得分:1)

您以JSON发送的不是Issue

首先,因为它不代表数据库持久性问题(这就是Issue类的目的),而是公共API期望其客户端提供的JSON结构。

第二,因为Issue没有名为user_id的任何字段,但是您的JSON却有。因此,请使用不同于Issue 类,该类实际上与API期望的JSON结构匹配,因此具有user_id属性。

然后使用此user_id通过User通过其ID查找UserRepository,并将User设置为您要创建的Issue

我也将user_id重命名为userId以遵守Java约定。

答案 1 :(得分:1)

您需要像这样发布json。

{
    "number": "3",
    "title": "Create an working service",
    "description": "The problem is that we do not have an working service.",
    "state": "NEW",
    "user":{
         "id":1
     }
}

这里有用户的连接列,因此对于用户实体,您必须传递用户ID。 您的保存将是这样。

 public void save(Issue issue) {
        if (issue == null)
            return;
        Issue actual = issueRepository.findByNumber(issue.getNumber());
        if (actual != null) {
            actual.setNumber(issue.getNumber());
            actual.setTitle(issue.getTitle());
            actual.setDescription(issue.getDescription());
            actual.setState(issue.getState());
            actual.setUser(userRepositoy.findById(issue.getUser().getId()));
            issueRepository.save(actual);
        } else {
            issueRepository.save(issue);
        }
    }

答案 2 :(得分:0)

user_id密钥未出现在您的Item类中。 根据实体映射, 请更改您的JSON。

{
"number": "3",
 "title": "Create an working service", "description": "The problem is that we do not have an working service.", 
"state": "NEW",
"user":{
"fullName":"USER",
"email":"user@gmail.com",
"username":"user"
}
}