0

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?

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
  • You are catching and swallowing an exception, log it and see why you get that request. I suspect due to some database constraint that is failing. – M. Deinum May 24 '15 at 11:41
  • Nah, I'm afraid you are not right. I tried to print `e.showMessage();` but nothing happens - failure is in signature `@RequestBody Task task` - he thinks the json coming from server is not correct. What is more, after logging out and logging in I am able to still create 1 task right (without restarting the server). It's not even running the function code. – Антон Коршунов May 24 '15 at 12:19
  • If you are still interested, I've replaced function body with `System.out.println(task.toString()); return new ResponseEntity(HttpStatus.OK);` , it still works for first input, but not for the following – Антон Коршунов May 24 '15 at 12:42
  • Then you are sending different requests, although you might not think so, there must be something different. If not it would be able to parse it. – M. Deinum May 24 '15 at 13:43
  • Using `@RequestBody String task` and `System.out.println(task);` provides this answer for two requests : `{"taskEventKeeper":{"id":3},"taskKeeper":{"id":1},"content":"123456"}` and `{"taskEventKeeper":{"id":3},"taskKeeper":{"id":1},"content":"qwerty"}`. All the same – Антон Коршунов May 24 '15 at 13:47
  • You are mapping any Exception thrown during saving on entity onto BAD REQUEST. Also instead of e.getMessage() (there is no Exception.showMessage() method) do: e.printStackTrace() and paste the results. – Rafal G. May 24 '15 at 15:38
  • The system out cannot be correct as for the second one you are getting a bad request which means it never reaches the controller... If it does, something else is going on... – M. Deinum May 25 '15 at 05:22

0 Answers0