V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
shayang888
V2EX  ›  Java

为什么 webflux 中无法以 x-www-form-urlencoded 解析 @RequestParam 的请求参数?

  •  
  •   shayang888 · 2023-02-27 19:38:19 +08:00 · 1012 次点击
    这是一个创建于 430 天前的主题,其中的信息可能已经有所发展或是发生改变。
    @PostMapping(value = "/demo", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
        public String responseEntityMono(@RequestParam("email") String email) {
            return email;
        }
    

    同样的一段代码,使用 postman 的 application/x-www-form-urlencoded 形式提交表单数据 email=xxx

    结果在普通的 web-starter 下创建的 springboot ,可以正常解析到 email ,而在 Webflux 下创建的 springboot ,则无论如何解析不了 email ,总提示我 email 字段缺失

    有人知道原因吗

    2 条回复    2023-02-27 21:13:09 +08:00
    lyxeno
        1
    lyxeno  
       2023-02-27 20:33:59 +08:00   ❤️ 2
    `@RequestParam`的文档里有说明
    在 Spring WebFlux 中`@RequestParam`只映射查询参数(URL 后面跟着的),而 x-www-form-urlencoded 是请求体的,自然不会解析

    https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html

    > In Spring MVC, "request parameters" map to query parameters, form data, and parts in multipart requests. This is because the Servlet API combines query parameters and form data into a single map called "parameters", and that includes automatic parsing of the request body.
    In Spring WebFlux, "request parameters" map to query parameters only. To work with all 3, query, form data, and multipart data, you can use data binding to a command object annotated with ModelAttribute.
    shayang888
        2
    shayang888  
    OP
       2023-02-27 21:13:09 +08:00
    @lyxeno 啊 原来如此 谢谢大佬!!!!!!!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1030 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 18:04 · PVG 02:04 · LAX 11:04 · JFK 14:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.