【问题标题】:Spring MVC - Why not able to use @RequestBody and @RequestParam togetherSpring MVC - 为什么不能同时使用 @RequestBody 和 @RequestParam
【发布时间】:2013-10-28 09:55:25
【问题描述】:

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

1) 仅@RequestBody

网址: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

网址:localhost:8080/SpringMVC/welcome
在正文中 - 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) 两者一起

网址: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) 上面的参数位置改变了

网址: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

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

【问题讨论】:

    标签: spring spring-mvc post http-post http-request-parameters


    【解决方案1】:

    @RequestBody javadoc 状态

    指示方法参数的注解应绑定到正文 网络请求。

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

    @RequestParam javadoc 状态

    表示方法参数应该绑定到的注解 一个网络请求参数。

    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 查询字符串中读取。它通常会将它们放在Map 中,但由于参数类型为String,Spring 会将Map 序列化为逗号分隔值。然后,@RequestBody 的处理程序再次从正文中读取任何内容。

    【讨论】:

    • 假设情况4),帖子正文是name=abc&age=2,那么根据你的解释名称应该是abc,正文应该是&age=2,但实际结果是名称是abc和正文是
    • @abhihello123 它读取整个 url 编码的表单参数。所以整个name=abc&age=2.
    • 但如果我再放一个@RequestParam,它会将 2 放入年龄。这意味着 name 只读取 abc。抱歉打扰了。
    • @abhihello123 是的,在这种情况下,完整的查询字符串已被读取,下一个@RequestParam 可以从中构建。参数存储在地图中,每次都可以检查请求参数。
    • @abhihello123 如果您有很多的耐心。下载源 jars 并在发送请求时使用调试器并逐步执行代码。你想看的类是RequestParamMethodArgumentResolver,它处理@RequestParam
    【解决方案2】:

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

    @RequestParam(required = false)String name
    

    【讨论】:

    • 是的,不会生成响应状态码 400。但无论如何他也无法获得@RequestParam 的值(如果它通过了)!
    【解决方案3】:

    发生这种情况是因为 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 lose params from POST body after read it once

    【讨论】:

      【解决方案4】:

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

      1. 和你的结果一样
      2. 和你的结果一样
      3. 给了body= "name=abc"name = "abc"
      4. 同 3。
      5. body ="name=abc", name = "xyz,abc"
      6. 同5。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-03-18
        • 2011-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-08
        相关资源
        最近更新 更多