我在前端使用了Backbone模型,模型URL为'Student',后端使用Spring Controller。
我可以使用以下代码进行提取
@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错误描述“客户端发送的请求在语法上是不正确的。”
骨干模型
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();
学生班
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;
}
}
答案 0 :(得分:0)
更新:问题是弹簧控制器的saveStudent()方法中JSON转换为学生对象的错误,同时接受给出400错误的@RequestBody参数。我通过在Student类中添加一个空的默认构造函数Student()并向Backbone save方法添加null来解决我的问题。
在学生班上
public Student(){
}
骨干保存
student.save(null,{
success:function()
{
...
}
);