lesstalkmorecode
lesstalkmorecode

Reputation: 167

spring post Failed to load resource: the server responded with a status of 400 (Bad Request)

I'm trying to send a json object to a spring controller by using react fetch. But I'm getting following error. Failed to load resource: the server responded with a status of 400 (Bad Request)

But get method can be called successfully.

http://localhost:8080/test/xx/getSprintTasks/${this.props.sprintNo}`

react fetch:

  fetch(`http://localhost:8080/test/xx/addSprintTasks/`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: task 
    })
    .then(response => {
        console.log(response)
        if (response.status >= 200 && response.status < 300) {
            alert('Successfully added')
            this.setState(prevState => ({
                toDoList: [
                    ...prevState.toDoList,
                    {
                        id: temp,
                        note: text
                    }
                ]
            }))
        } else {
            alert('Somthing happened wrong')
        }
    })
    .catch(err => err)

my body:

   const task = JSON.stringify({
        sprintNo: "1",
        id: "1",
        note: "text",
        boardName: "todo"
    })  

I also tried as following:

  const task = JSON.stringify({
        "sprintNo": "1",
        "id": "1",
        "note": "text",
        "boardName": "todo"
    }) 

My Controller:

 @Controller
 @RequestMapping("/xx")
 public class TestController {

    @RequestMapping(value = "/getSprintTasks/{sprintno}", method = RequestMethod.GET, produces="application/json")
    public @ResponseBody Board getSprintTasks(@PathVariable String sprintno) {
    Board board = new Board();
    board.setSprintNo(sprintno);
    board.setUniqueId(56);
    board.setDoneList(new Note[]{new Note(3, "deneme"), new Note(4, "hello wrold")});
    board.setToDoList(new Note[]{new Note(1, "deneme"), new Note(2, "hello wrold")});

    return board;
    }


    @RequestMapping(value="/addSprintTasks", method = RequestMethod.POST, consumes="application/json")
    public void addSprintTasks(@RequestBody Task task) {
    System.out.println(task.getBoardName());
    System.out.println(task.getId());
    System.out.println(task.getNote());
    System.out.println(task.getSprintNo());
    }

}

Task.java:

public class Task {    
private String sprintNo;
private String id;
private String note;
private String boardName;

public Task(String sprintNo, String id, String note, String boardName) {
super();
this.sprintNo = sprintNo;
this.id = id;
this.note = note;
this.boardName = boardName;
}

public String getSprintNo() {
    return sprintNo;
}
public void setSprintNo(String sprintNo) {
    this.sprintNo = sprintNo;
}
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getNote() {
    return note;
}
public void setNote(String note) {
    this.note = note;
}
public String getBoardName() {
    return boardName;
}
public void setBoardName(String boardName) {
    this.boardName = boardName;
}


}    

Upvotes: 1

Views: 6597

Answers (2)

Ubercool
Ubercool

Reputation: 1021

Your Task.java is not a POJO. Provide a default constructor implementation and try again. You may need to clean the project sometimes for changes to take effect.

Upvotes: 2

Raphael Alves
Raphael Alves

Reputation: 189

Your post url on react is http://localhost:8080/test/xx/addSprintTasks/ but on the spring controller is http://localhost:8080/test/xx/addSprintTasks

Remove the last slash on javascript or add it on the value of the @RequestMapping annotation on java.

Upvotes: 0

Related Questions