我的客户端javascript应用程序使用backbone.js。这是我发布JSON的地方:
function testREST() {
var model;
cfd.init($('#email').val() + ':');
model = new cfd.UserModel({
email: 'test@example.com',
fName: 'Test',
lName: 'Dude',
heardHow: 21,
isDeleted: 0
});
model.save();
}
这是POST到我的控制器,但由于控制器参数而获得400.
这是JSON有效负载:
{ "email" : "test@example.com",
"fName" : "Test",
"heardHow" : 21,
"isDeleted" : 0,
"lName" : "Dude"
}
这是控制器:
@Controller
public class PostController {
@Inject
private UserRepository users;
@RequestMapping(value = "/app/user", method = RequestMethod.POST)
public @ResponseBody void insertUser(@RequestBody User user) {
System.out.println("POST SAYS HI: " + user.getfName());
users.save(user);
}
}
如果我删除:
@RequestBody User user
但我自然需要它。
用户类:
@Entity
@Table(name="users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private long id;
@Column(name="email")
private String email = "";
@Column(name="fname")
private String fName = "";
@Column(name="lname")
private String lName = "";
@Column(name="heardhow")
private int heardHow = 0;
@Column(name="isdeleted")
private boolean isDeleted = false;
public User() {
}
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public int getHeadHow() {
return heardHow;
}
public void setHeadHow(int headHow) {
this.heardHow = headHow;
}
public boolean isDeleted() {
return isDeleted;
}
public void setDeleted(boolean isDeleted) {
this.isDeleted = isDeleted;
}
}
我有以下maven依赖项:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
我还在spring-config.xml中包含以下内容:
<mvc:annotation-driven />
我正在模仿我的实现:
http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/htmlsingle/#mvc-ann-requestmapping-consumes
如何解决此问题?
答案 0 :(得分:0)
您的User
课程中有拼写错误。属性headHow
应为heardHow
。 JSON使用属性heardHow
。错误可能导致杰克逊映射器在数据绑定时失败 - 导致400。
更新:当原始问题包含User