@RequestBody仅在第二次失败

时间:2015-05-24 09:14:58

标签: json spring mapping request-mapping

我使用@RequestBody表示法从前端获取JSON并将任务添加到数据库。看起来像这样

 @RequestMapping(method = RequestMethod.POST)
    public @ResponseBody ResponseEntity<Task> addTask(@RequestBody Task task) {
        try {
            tService.saveTask(task);
            return new ResponseEntity<Task>(task, HttpStatus.OK);
        }
        catch (final Exception e) {
            return new ResponseEntity<Task>(HttpStatus.BAD_REQUEST);
        }
    }

这首次很有用 - 任务是在数据库中创建并返回200个代码。但如果我继续添加任务,则返回400和

 The request sent by the client was syntactically incorrect.

Task.java代码:

@Entity
@Table(name = "task")
public class Task {
    private int id;
    private String content;
    private Participant taskKeeper;
    private Event taskEventKeeper;
    private Boolean isDone;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "task_id")
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Column(name = "content", length = 500, nullable = false)
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }


    @Column(name = "isDone", nullable = false)
    public Boolean getIsDone() { return isDone; }
    public void setIsDone(Boolean isDone) { this.isDone = isDone; }


    @ManyToOne
    @JsonBackReference("task-event")
    @JoinColumn(name = "event_id")
    public Event getTaskEventKeeper() {
        return taskEventKeeper;
    }
    public void setTaskEventKeeper(Event taskEventKeeper) {
        this.taskEventKeeper = taskEventKeeper;
    }

    @ManyToOne
    @JoinColumn(name = "participant_id")
    public Participant getTaskKeeper() {
        return taskKeeper;
    }
    public void setTaskKeeper(Participant taskKeeper) {
        this.taskKeeper = taskKeeper;
    }

    public Task(String content) {
        isDone = false;
        this.content = content;
    }

    public Task(String content, Participant taskKeeper, Event taskEventKeeper) {
        this.content=content;
        this.taskEventKeeper=taskEventKeeper;
        this.taskKeeper=taskKeeper;
        this.isDone=false;
    }

    public Task() {
        isDone=false;
    }

    @Override
    public String toString() {
        return "{" +
                "\"id\":" + id +
                ", \"content\":\"" + content + '\"' +
                '}';
    }
}

来自客户端的JSON总是如下所示:

{"taskEventKeeper":{"id":3},"taskKeeper":{"id":1},"content":"Bazinga"}

如何摆脱这个?

0 个答案:

没有答案