Spring没有在POST方法中传递@RequestParam参数

Joh*_*lly 7 spring spring-mvc

我遇到了Spring和post请求的问题.我正在为Ajax调用设置一个控制器方法,请参阅下面的方法定义

@RequestMapping(value = "add.page", method = RequestMethod.POST)
@ResponseBody
public Object createComment(
        @RequestParam(value = "uuid", required = false) String entityUuid,
        @RequestParam(value = "type", required = false) String entityType,
        @RequestParam(value = "text", required = false) String text,
        HttpServletResponse response) {
        ....
Run Code Online (Sandbox Code Playgroud)

无论我以何种方式进行HTML调用,@RequestParam参数的值始终为null.我有很多其他方法看起来像这样,主要区别在于其他方法是GET方法,而这一方法是POST.是不是可以使用@RequestParamPOST方法?

我正在使用Spring版本3.0.7.RELEASE - 有谁知道问题的原因可能是什么?


Ajax代码:

$.ajax({
    type:'POST',
    url:"/comments/add.page",
    data:{
        uuid:"${param.uuid}",
        type:"${param.type}",
        text:text
    },
    success:function (data) {
        //
    }
});
Run Code Online (Sandbox Code Playgroud)

Joh*_*lly 19

问题原来是我调用方法的方式.我的ajax代码传递了请求体中的所有参数而不是请求参数,所以这就是我的@RequestParam参数全部为空的原因.我将我的ajax代码更改为:

$.ajax({
    type: 'POST',
    url: "/comments/add.page?uuid=${param.uuid}&type=${param.type}",
    data: text,
    success: function (data) {
        //
    }
});
Run Code Online (Sandbox Code Playgroud)

我还改变了我的控制器方法来从请求体中获取文本:

@RequestMapping(value = "add.page", method = RequestMethod.POST)
@ResponseBody
public Object createComment(
        @RequestParam(value = "uuid", required = false) String entityUuid,
        @RequestParam(value = "type", required = false) String entityType,
        @RequestBody String text,
        HttpServletResponse response) {
Run Code Online (Sandbox Code Playgroud)

而现在我正在按照我的预期获得参数.