Backbone 模型保存 Spring 后端控制器不工作
Backbone Model save with Spring Controller at the back end not working
我在前端使用 Backbone 模型,在后端使用模型 url 作为 'Student' 和 Spring 控制器。
我可以使用以下代码进行提取
@RequestMapping(value = "/Student", produces = "application/json", method = RequestMethod.GET)
public @ResponseBody Student createStudent() {
Student student = new Student("Abc","Def");
return student;
}
但是下面的保存代码不起作用
@RequestMapping(value = "/Student", consumes = "application/json", produces = "application/json",method = RequestMethod.POST)
public @ResponseBody Student saveStudent(@RequestBody Student student) {
System.out.println(student.getName());
return student;
}
我已经在客户端更改了 Student 的一些属性并保存了但是它给我 HTTP 400 错误描述 "The request sent by the client was syntactically incorrect."
Backbone 型号
var Student = Backbone.Model.extend({
url : 'Student',
defaults : {
name : '',
address : '',
}
});
var student = new Student();
student.fetch();
alert(JSON.stringify(student,null,4));
student.set({name:'john'});
student.save();
学生Class
public class Student{
private String name;
private String address;
public void Student(String name,String address){
this.name=name;
this.address=address;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address=address;
}
}
更新:问题是在 spring 控制器的 saveStudent() 方法中将 JSON 错误转换为学生对象,同时接受 @RequestBody 参数给出 400 错误。我通过在 Student class 中添加一个空的默认构造函数 Student() 并将 null 添加到 Backbone 保存方法来解决我的问题。
在学生中class
public Student(){
}
Backbone保存
student.save(null,{
success:function()
{
...
}
);
我在前端使用 Backbone 模型,在后端使用模型 url 作为 'Student' 和 Spring 控制器。
我可以使用以下代码进行提取
@RequestMapping(value = "/Student", produces = "application/json", method = RequestMethod.GET)
public @ResponseBody Student createStudent() {
Student student = new Student("Abc","Def");
return student;
}
但是下面的保存代码不起作用
@RequestMapping(value = "/Student", consumes = "application/json", produces = "application/json",method = RequestMethod.POST)
public @ResponseBody Student saveStudent(@RequestBody Student student) {
System.out.println(student.getName());
return student;
}
我已经在客户端更改了 Student 的一些属性并保存了但是它给我 HTTP 400 错误描述 "The request sent by the client was syntactically incorrect."
Backbone 型号
var Student = Backbone.Model.extend({
url : 'Student',
defaults : {
name : '',
address : '',
}
});
var student = new Student();
student.fetch();
alert(JSON.stringify(student,null,4));
student.set({name:'john'});
student.save();
学生Class
public class Student{
private String name;
private String address;
public void Student(String name,String address){
this.name=name;
this.address=address;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address=address;
}
}
更新:问题是在 spring 控制器的 saveStudent() 方法中将 JSON 错误转换为学生对象,同时接受 @RequestBody 参数给出 400 错误。我通过在 Student class 中添加一个空的默认构造函数 Student() 并将 null 添加到 Backbone 保存方法来解决我的问题。
在学生中class
public Student(){
}
Backbone保存
student.save(null,{
success:function()
{
...
}
);