68

使用带有 Post 请求和 Content-Type application/x-www-form-urlencoded 的 HTTP 开发客户端

1) 只有@RequestBody

URL:localhost:8080/SpringMVC/welcome
正文:name=abc

@RequestMapping(method = RequestMethod.POST)
public String printWelcome(@RequestBody String body, Model model) {
    model.addAttribute("message", body);
    return "hello";
}
// Gives body as 'name=abc' as expected

2)只有@RequestParam

URL: localhost:8080/SpringMVC/welcome
In Body - name=abc

@RequestMapping(method = RequestMethod.POST)
public String printWelcome(@RequestParam String name, Model model) {
    model.addAttribute("name", name);
    return "hello";
}
// Gives name as 'abc' as expected

3) 两者一起

URL:localhost:8080/SpringMVC/welcome
正文:name=abc

@RequestMapping(method = RequestMethod.POST)
public String printWelcome(
    @RequestBody String body, 
    @RequestParam String name, Model model) 
{
    model.addAttribute("name", name);
    model.addAttribute("message", body);
    return "hello";
}
// HTTP Error Code 400 - The request sent by the client was syntactically incorrect.

4)以上参数位置已更改

URL:localhost:8080/SpringMVC/welcome
正文:name=abc

@RequestMapping(method = RequestMethod.POST)
public String printWelcome(
    @RequestParam String name, 
    @RequestBody String body, Model model) 
{
    model.addAttribute("name", name);
    model.addAttribute("message", body);
    return "hello";
}
// No Error. Name  is 'abc'. body is empty

5)一起但获取类型url参数

网址:localhost:8080/SpringMVC/welcome?name=xyz 正文
:name=abc

@RequestMapping(method = RequestMethod.POST)
public String printWelcome(
    @RequestBody String body, 
    @RequestParam String name, Model model) 
{
    model.addAttribute("name", name);
    model.addAttribute("message", body);
    return "hello";
}
// name is 'xyz' and body is 'name=abc'

6) 与 5) 相同,但参数位置已更改

@RequestMapping(method = RequestMethod.POST)
public String printWelcome(
    @RequestParam String name, 
    @RequestBody String body, Model model) 
{
    model.addAttribute("name", name);
    model.addAttribute("message", body);
    return "hello";
}
// name = 'xyz,abc' body is empty

有人可以解释这种行为吗?

4

4 回答 4

53

javadoc@RequestBody状态

指示方法参数的注释应绑定到 Web 请求的主体。

它使用已注册的实例HttpMessageConverter将请求正文反序列化为带注释的参数类型的对象。

@RequestParamjavadoc 状态

指示方法参数应绑定到 Web 请求参数的注释。

  1. Spring 将请求的主体绑定到带有注释的参数@RequestBody

  2. Spring 将请求正文中的请求参数(url 编码的参数)绑定到您的方法参数。Spring 将使用参数的名称,即。name, 映射参数。

  3. 参数按顺序解析。首先@RequestBody处理。Spring 将消耗所有HttpServletRequest InputStream. 然后,当它尝试解析@RequestParam默认情况下required的 时,查询字符串中没有请求参数或请求正文的剩余部分,即。没有什么。因此它以 400 失败,因为处理程序方法无法正确处理请求。

  4. 处理程序@RequestParam首先执行,读取它可以HttpServletRequest InputStream映射请求参数的内容,即。整个查询字符串/url 编码的参数。它这样做并获取abc映射到参数的值name。当处理程序@RequestBody运行时,请求正文中没有任何内容,因此使用的参数是空字符串。

  5. 处理程序@RequestBody读取正文并将其绑定到参数。然后,处理程序@RequestParam可以从 URL 查询字符串中获取请求参数。

  6. @RequestParam从正文和 URL 查询字符串读取的处理程序。它通常会将它们放在 a 中Map,但由于参数是 type String,Spring 会将它们序列Map化为逗号分隔的值。then的处理程序@RequestBody再一次没有从正文中读取任何内容。

于 2013-10-19T18:53:45.093 回答
6

现在回答这个问题为时已晚,但它可能对新读者有所帮助,似乎是版本问题。我用 spring 4.1.4 运行了所有这些测试,发现 and 的顺序@RequestBody无关紧要@RequestParam

  1. 和你的结果一样
  2. 和你的结果一样
  3. 给了body= "name=abc", 和name = "abc"
  4. 与 3 相同。
  5. body ="name=abc",name = "xyz,abc"
  6. 与 5 相同。
于 2015-01-06T22:16:56.950 回答
1

发生这种情况是因为 Servlet 规范不是很直接。如果您使用的是本机HttpServletRequest实现,则无法同时获得 URL 编码正文和参数。Spring 做了一些变通方法,这使得它更加奇怪和不透明。

在这种情况下,Spring(3.2.4 版)使用方法中的数据为您重新呈现主体getParameterMap()。它混合了 GET 和 POST 参数并打破了参数顺序。负责混乱的班级是ServletServerHttpRequest。不幸的是它不能被替换,但类StringHttpMessageConverter可以。

不幸的是,干净的解决方案并不简单:

  1. 更换StringHttpMessageConverter. 复制/覆盖原来的类调整方法readInternal()
  2. 包装HttpServletRequest覆盖getInputStream()getReader()getParameter*()方法。

在方法 StringHttpMessageConverter#readInternal 中必须使用以下代码:

    if (inputMessage instanceof ServletServerHttpRequest) {
        ServletServerHttpRequest oo = (ServletServerHttpRequest)inputMessage;
        input = oo.getServletRequest().getInputStream();
    } else {
        input = inputMessage.getBody();
    }

然后转换器必须在上下文中注册。

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true/false">
        <bean class="my-new-converter-class"/>
   </mvc:message-converters>
</mvc:annotation-driven>

这里描述了第二步:Http Servlet request lost params from POST body after read it once

于 2014-09-25T09:37:39.570 回答
0

您也可以将 @RequestParam 默认所需状态更改为 false,以便不会生成 HTTP 响应状态代码 400。这将允许您以您喜欢的任何顺序放置注释。

@RequestParam(required = false)String name
于 2013-12-29T05:56:15.050 回答