如何在spring4中调试请求



我在spring中有一个应用程序,它使用spring-rest和spring-mvc。我有一个控制器,它在POST上添加了一个注释。方法如下:

@RequestMapping(method = RequestMethod.POST)
public List<Comment> addComment(Comment comment) {
    return service.addComment(comment);
}

我发送了一个类型为"application/json"的请求,其中数据集为{author:"text","text":"commentText"},当我在该方法内部进行调试时,我在这两个属性上都得到了null值。这是我的评论模型:

@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,我会收到一个请求,并使用一些json库(如jackson或gson)自己创建一个Comment对象,但这里我有点受阻,因为所有操作都是使用注释完成的。有什么想法吗?

您可能想要用@RequestBody注释方法参数,以告诉Spring从哪里填充它。

最新更新