繁体   English   中英

在spring mvc3中同时使用@RequestBody和@RequestParam

[英]Using both @RequestBody and @RequestParam together in spring mvc3

我使用spring-mvc 3.1.0.RELEASE ,由于某种原因,使用查询参数和请求体映射POST不起作用。

以下是我的控制器方法的外观:

  @RequestMapping(method = POST, value = "/post-to-me/") 
  public void handlePost(
    @RequestBody Content content,
    @RequestParam("param1") String param1,
    @RequestParam("param2") String param2
  ){
       //do stuff       
  }

但是,如果我将所有请求参数转换为路径参数,则映射有效。 有没有人碰到类似的东西?

谢谢!

编辑:“不起作用”== 404当我尝试做, POST /post-to-me?param1=x&param2=y

首先,你的POST网址与控制器方法网址不匹配,你的网址必须是“/ post-to-me /?param1 = x&param2 = y”not“/ post-to-me?param1 = x&param2 = y”

第二,Content类来自哪里? 我使用了一个字符串,对我来说很好

@RequestMapping(method = RequestMethod.POST, value = "/post-to-me/")
public void handlePost(@RequestBody String content,
        @RequestParam("param1") String param1,
        @RequestParam("param2") String param2, HttpServletResponse response) {
    System.out.println(content);
    System.out.println(param1);
    System.out.println(param2);
    response.setStatus(HttpServletResponse.SC_OK);
}

请注意,我使用HttpServletResponse返回HTTP 200代码,但我认为返回Http代码有更好的解决方案,请检查: Spring MVC中的多响应http状态

请求映射值末尾的斜杠可能是问题所在。 尝试:

@RequestMapping(method = RequestMethod.POST, value = "/post-to-me")

或发送你的POST请求POST /post-to-me/?param1=x&param2=y

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM