我在春天有一个应用程序,它使用弹簧休息和弹簧mvc。我有一个控制器,在POST上添加评论。方法如下所示:
@RequestMapping(method = RequestMethod.POST)
public List<Comment> addComment(Comment comment) {
return service.addComment(comment);
}
我发送的类型&#34; application / json&#34;数据设置为{author:&#34; text&#34;,&#34; text&#34;:&#34; commentText&#34;},当我调试该方法时,我得到两个属性上的空值。 这是我的评论模型:
@Entity
public class Comment {
@Id
@GeneratedValue
private Long id;
private String author;
private String text;
public Comment() {
}
public Comment(String author, String text) {
this.author = author;
this.text = text;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@Override
public String toString() {
return "Comment{" +
"id=" + id +
", author='" + author + '\'' +
", text='" + text + '\'' +
'}';
}
}
我想知道如何调试,出了什么问题?使用经典的servlet,我会得到一个请求,我会自己创建一个Comment对象,使用一些像jackson或gson这样的json库,但是这里我有点被阻止,因为一切都是使用注释完成的。有什么想法吗?
答案 0 :(得分:1)
你可能想用@RequestBody
注释方法参数,告诉Spring在哪里填充它。