美文网首页java
Springboot 接收参数的几种常用方式

Springboot 接收参数的几种常用方式

作者: bianruifeng | 来源:发表于2021-07-10 18:40 被阅读0次

    Springboot Controller 接收参数

    接收参数的几种常用方式

    注解

    @PathVariable

    @RequestParam

    @RequestBody

    注解 支持类型 支持请求类型 支持的Content-Type 请求示例
    @PathVariable url Get all /orders/{id}
    @RequestParam url Get all /orders?id=abc
    Body Post/Put/Delete/Patch form-data,x-www.form-urlencoded
    @RequestBody Body Post/Put/Delete/Patch json {"name":"Zhang ","age":18}

    一、对于一些非必填参数,可以使用required 关键字来标识,同时必须设置默认值defaultValue

    如getOrder方法中对price 参数的获取:

    /**
         * Get请求的参数可以通过@PathVariable和@RequestParam获取
         * @param id 必填
         * @param name 必填
         * @param price 选填,默认值为0
         * @return
         */
    @GetMapping("/orders/{id}")
    public String getOrder(@PathVariable(value = "id")Integer id,
                           @RequestParam(value = "name")String name,
                           @RequestParam(value = "price",required = false,defaultValue = "0") Integer price)
    {
      String result = "id:"+id+",name:"+name+",price:"+price;
      return result;
    }
    

    二、参数可以不配注解直接和Entity类绑定,但是不支持json格式,仅支持form-data和x-www.form-urlencoded格式

    @PostMapping("/order/")
    public String addorder(Order order)
    

    第一类:请求路径参数

    仅支持Get请求:

    1. @PathVariable

    获取请求路径。例如:url/{id} 形式。

    1. @RequestParam

    获取查询参数。例如:url?id=123&name=zhangsan 形式。

    范例:

    GET请求
    http://localhost:8080/demo/123?name=suki_rong
    

    服务端Java代码

    @GetMapping("/demo/{id}")
    public void demo(@PathVariable(name = "id") String id, @RequestParam(name = "name") String name) {
        System.out.println("id="+id);
        System.out.println("name="+name);
    }
    //PathVariable匹配路径参数,RequestParam匹配问好后参数
    

    第二类:Body 参数

    支持Post/Put/Delete/Patch请求

    1. @RequestBody

    请求:

    #post请求
    #参数:
    {
      "name":"zhangsan",
      "age":18
    }
    #参数类型:
    `Content-Type=application/json`
    #api:
    http://localhost:8080/demo1
    

    (一)服务器代码:

    @PostMapping("/demo1")
    public void demo1(@RequestBody User user) {
        System.out.println(user.toString());
    }
    

    输出:

    User{name='zhangsan', age=18}
    

    (二)服务器代码:

    @PostMapping("/demo1")
    public void demo1(@RequestBody Map<String, String>user) {
        System.out.println(user.get("name"));
    }
    

    输出:

    zhangsan
    
    1. 无注解
    #post请求
    #参数:
    {
      "name":"zhangsan",
      "age":18
    }
    #参数类型:
    `Content-Type=form-data`
    #api:
    http://localhost:8080/demo2
    

    服务器代码:

    @PostMapping("/demo2")
    public void demo2(User user) {
        System.out.println(user.toString());
    }
    

    输出:

    User{name='zhangsan', age=18}
    

    其他参数类型:

    content-type:x-www-form-urlencoded

    multipart/form-datax-www-fom-urlencoded的区别:

    • multipart/form-data:可以上传文件或者键值对,最后都会转化成一条消息。
    • x-www-fom-urlencoded:只能上传键值对,而且键值对都是通过&间隔分开的。

    第三类:请求头参数以及Cookie

    1. @RequestHeader
    2. @CookieValue

    范例:

    java代码:

    @GetMapping("/demo3")
    public void demo3(@RequestHeader(name = "myHeader") String myHeader,
            @CookieValue(name = "myCookie") String myCookie) {
        System.out.println("myHeader=" + myHeader);
        System.out.println("myCookie=" + myCookie);
    }
    

    @GetMapping("/demo3")
    public void demo3(HttpServletRequest request) {
        System.out.println(request.getHeader("myHeader"));
        for (Cookie cookie : request.getCookies()) {
            if ("myCookie".equals(cookie.getName())) {
                System.out.println(cookie.getValue());
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:Springboot 接收参数的几种常用方式

        本文链接:https://www.haomeiwen.com/subject/koacpltx.html