I use @RequestBody notation to get a JSON from front-end and to add task to database. Looks like this
@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);
}
}
This works great for the first time - task is creating in database and return 200 code. But if I continue adding tasks it returns 400 and
The request sent by the client was syntactically incorrect.
Task.java code :
@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 + '\"' +
'}';
}
}
JSONs coming from client is always look like this :
{"taskEventKeeper":{"id":3},"taskKeeper":{"id":1},"content":"Bazinga"}
How to get rid of this?