zoukankan      html  css  js  c++  java
  • SpringMVC请求参数的获取方式


    一、GET请求参数获取

    get请求参数,一般都是直接挂在请求的url上。

    1. 通过HttpServletRequest获取参数

    通过javax.servlet.ServletRequest#getParameter 来获取对应的参数:

    @RestController
    @RequestMapping(path = "webs/demo")
    public class DemoController {
    
        @RequestMapping(path = "req1")
        public String req1(HttpServletRequest request) {
            String user = request.getParameter("user");
            String password = request.getParameter("password");
            return "req1 user: " + user + " pwd: " + password;
        }
    }
    • 这是一个最基本的获取参数的方式,get,post请求都适用的,通常在filter,intercepter中也是可以通过HttpServletRequest对象来获取请求参数
    • HttpServletRequest可以获取请求头的完整信息
    • 在一次请求的生命周期内,可以通过下面的方式获取Request对象(当然也可以获取response对象)。
    HttpServletRequest httpServletRequest = ((ServletRequestAttributes) 
        RequestContextHolder.getRequestAttributes()).getRequest();

    2. 直接方法参数获取

    @RequestMapping(path = "req2")
    public String req2(String user, String password) {
        return "req2 user: " + user + " pwd: " + password;
    }

    请求验证:

    http://127.0.0.1:8080/webs/demo/req2?user=%E5%B0%8F%E7%81%B0%E7%81%B0Blog&password=123456
    ## 输出: req2 user: 小灰灰Blog pwd: 123456
    
    
    http://127.0.0.1:8080/webs/demo/req2?password=123456
    ## 输出: req2 user: null pwd: 123456
    
    
    http://127.0.0.1:8080/webs/demo/req2?password=123456&User=blog
    ## 输出: req2 user: null pwd: 123456
    • 将url参数映射到了Controller方法的参数上
    • 方法参数名必须和url参数名完全一致(区分大小写)
    • 顺序无关
    • 若参数没传,则默认为null
    • 如果请求参数与方法参数类型不一致,会抛出转换异常
    • 如果方法参数为非封装基本类型,则url参数必须存在,否则报错

    3. RequestParam注解方式获取请求参数

    通过@RequestParam注解获取参数,与直接方法参数获取方法一样。

    @RequestMapping(path = "req3", method = RequestMethod.GET)
    public String req3(@RequestParam("user") String username,
                       @RequestParam("password") String pwd) {
        return "req3 user: " + username + " pwd: " + pwd;
    }

    测试用例:

    # case1 
    http://127.0.0.1:8080/webs/demo/req3?password=123456&user=blog
    ## 输出: req3 user: blog pwd: 123456
    
    
    # case2
    http://127.0.0.1:8080/webs/demo/req3?password=123456
    ## 输出:报错, Required String parameter 'user' is not presen
    • 不指定注解的name或value属性时,等同于第二种使用姿势
    • 注解的name属性或value属性,用实际的参数名来指定
    • controller的参数名与url参数名没有强关联(区别第二种方式)
    • 参数类型需要保证一致(同第二种方式)
    • 如果url参数可选,请设置require属性为false,如下
    @RequestParam(name = "user",  required = false) String username

    4. Bean方式获取参数

    请求参数比较复杂的情况下,这种方式较好:

    @Data
    public static class UserDO {
        String user;
        String password;
    }
    
    @RequestMapping(path = "req4", method = RequestMethod.GET)
    public String req4(UserDO userDO) {
        return "req4 userDO: " + userDO;
    }

    测试用例:

    # case1
    http://127.0.0.1:8080/webs/demo/req4?password=123456&user=%E5%B0%8F%E7%81%B0%E7%81%B0Blog
    ## 输出: req4 userDO: DemoController.UserDO(user=小灰灰Blog, password=123456)
    
    
    # case2
    http://127.0.0.1:8080/webs/demo/req4?password=123456
    ## 输出: req4 userDO: DemoController.UserDO(user=null, password=123456)
    • 定义一个bean,内部属性和请求参数对应
    • 允许参数不存在的情况,会使用null代替(所以,尽量不要使用非封装基本类型,否则参数不传时,会抛异常)
    • bean的属性,可以根据实际情况指定类型

    5. ModelAttribute注解方式

    @ModelAttribute注解的方法,会优于Controller之前执行,一般更常见于向视图传输数据使用。

    6. Path参数

    Path参数,专指的是请求路径的参数,如

    http://127.0.0.1:8080/webs/demo/req4?password=123456

    上面这个url中,password是我们传统意义上的请求参数,其中path参数则是指其中 req4, demo这种path路径中的一环;

    一般path参数的获取方式如下

    @RequestMapping(path = "req6/{user}/info")
    public String req6(@PathVariable(name = "user") String user) {
        return "req6 user: " + user;
    }
    • path参数的使用,需要确保参数存在且类型匹配
    • path参数和url参数不会相互影响

    二、POST请求参数获取

    POST请求参数,更多的是看提交表单参数是否可以获取到,以及如何获取。

    2.1 HttpServletRequest方式获取参数

    测试:

    # case1 
    curl -d "user=小灰灰Blog&password=123456" "http://127.0.0.1:8080/webs/demo/req1"
    ## 输出: req1 user: 小灰灰Blog pwd: 123456
    
    # case2
    curl -d "user=小灰灰Blog" "http://127.0.0.1:8080/webs/demo/req1?password=123456"
    ## 输出:req1 user: 小灰灰Blog pwd: 12345
    
    
    # case3
    curl -d "user=小灰灰Blog" "http://127.0.0.1:8080/webs/demo/req1?user=greyBlog"
    ## 输出:req1 user: greyBlog pwd: null

    curl也可以换成js请求测试方式

    var formData = new FormData();
    formData.append("user", "小灰灰Blog");
    
    $.ajax({
      url: 'http://127.0.0.1:8080/webs/demo/req1?password=123456',
      type: 'post',
      cache: false,
      data: formData,
      processData: false,
      contentType: false
    });
    • 对于HttpServletReuqest方式获取参数时,get和post没什么区别
    • 若url参数和表单参数同名了,测试结果显示使用的是url参数。

    2.2 方法参数获取

    # case 1
    curl -d "user=小灰灰Blog&password=123456" "http://127.0.0.1:8080/webs/demo/req2"
    ## 输出: req2 user: 小灰灰Blog pwd: 123456
    
    
    # case 2
    curl -d "password=123456" "http://127.0.0.1:8080/webs/demo/req2"
    ## 输出:req2 user: null pwd: 123456
    
    
    # case 3
    curl -d "password=123456" "http://127.0.0.1:8080/webs/demo/req2?user=blog"
    ## 输出: req2 user: blog pwd: 123456

    2.3 RequestParam注解方式

    # case 1
    curl -d "password=123456&user=blog" "http://127.0.0.1:8080/webs/demo/req3"
    ## 输出: req3 user: blog pwd: 123456
    
    
    # case 2
    curl -d "password=123456" "http://127.0.0.1:8080/webs/demo/req3?user=blog"
    ## 输出: req3 user: blog pwd: 123456
    
    
    # case 3
    curl -d "password=123456&user=blog" "http://127.0.0.1:8080/webs/demo/req3?password=900"
    ## 输出:req3 user: blog pwd: 900,123456
    • 和前面的两种方式不同的是,当post表单的参数和url参数同名时,会合并成一个字符串

    2.4 Bean方式

    ## case1 
    curl -d "password=123456&user=blog" "http://127.0.0.1:8080/webs/demo/req4?password=900"
    ## 输出 req4 userDO: DemoController.UserDO(user=blog, password=900,123456)
    
    
    ## case2
    curl -d "password=123456&user=blog" "http://127.0.0.1:8080/webs/demo/req4"
    ## 输出 req4 userDO: DemoController.UserDO(user=blog, password=123456)
    
    
    ## case3
    curl -d "password=123456" "http://127.0.0.1:8080/webs/demo/req4"
    ## 输出 req4 userDO: DemoController.UserDO(user=null, password=123456)

    2.5 PathVariable

    三、多媒体上传参数获取

    @RequestMapping(path = {"wx/upload", "wx/wx/upload"}, method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.OPTIONS})
    @ResponseBody
    public String upload(HttpServletRequest request) {
        MultipartFile file = null;
        if (request instanceof MultipartHttpServletRequest) {
            file = ((MultipartHttpServletRequest) request).getFile("image");
        }
    
        if (file == null) {
            throw new IllegalArgumentException("图片不能为空!");
        }
    
        return "success";
    }
    • 主要是利用HttpServletRequest来获取上传的文件
    • 如果接口必须要求上传文件,可以直接把参数声明为 MultipartHttpServletRequest, 此时调用方如果不传参数,会被异常拦截(可以通过@ControllerAdvice来拦截全局异常)
    • 如果可以不上传文件,则可以用上面的这种猥琐姿势,内部进行判断
    • ((MultipartHttpServletRequest) request).getFile(xxx)来获取指定名的上传文件

    四、小结

    五种方法获取参数:
    参数



    原文:
    灰灰Blog

  • 相关阅读:
    bzoj 2152: 聪聪可可
    bzoj 2143: 飞飞侠
    bzoj 2132: 圈地计划
    bzoj 2127: happiness
    bzoj 2124: 等差子序列
    bzoj 2120: 数颜色
    对MySQL数据类型的认识
    MySQL详解--锁,事务(转)
    mysql 5.7快速部署
    elasticsearch报错[WARN ][bootstrap ] Unable to lock JVM Memory: error=12,reason=Cannot allocate memory,解决
  • 原文地址:https://www.cnblogs.com/aixing/p/13327508.html
Copyright © 2011-2022 走看看