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

Deadly 提交于 2021-01-28 05:15:26

问题


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;
}


}    

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/54686071/spring-post-failed-to-load-resource-the-server-responded-with-a-status-of-400

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!