• SpringBoot实战(四)获取接口请求中的参数(@PathVariable,@RequestParam,@RequestBody)


    上一篇SpringBoot实战(二)Restful风格API接口中写了一个控制器,获取了前端请求的参数,现在我们就参数的获取与校验做一个介绍:

    一:获取参数

    SpringBoot提供的获取参数注解包括:@PathVariable,@RequestParam,@RequestBody,三者的区别如下表:

    示例代码:

    Order:

     1 package com.example.demo.controller.user.entity;
     2 
     3 public class Order {
     4     private Integer id;
     5     private String name;
     6     private Integer price;
     7 
     8     public Integer getId() {
     9         return id;
    10     }
    11 
    12     public void setId(Integer id) {
    13         this.id = id;
    14     }
    15 
    16     public String getName() {
    17         return name;
    18     }
    19 
    20     public void setName(String name) {
    21         this.name = name;
    22     }
    23 
    24     public Integer getPrice() {
    25         return price;
    26     }
    27 
    28     public void setPrice(Integer price) {
    29         this.price = price;
    30     }
    31 }

    OrderController

     1 package com.example.demo.controller.user.controller;
     2 
     3 import com.example.demo.controller.user.entity.Order;
     4 import org.springframework.web.bind.annotation.*;
     5 
     6 @RestController
     7 public class OrderController {
     8 
     9     /**
    10      * Get请求的参数可以通过@PathVariable和@RequestParam获取
    11      * @param id 必填
    12      * @param name 必填
    13      * @param price 选填,默认值为0
    14      * @return
    15      */
    16     @GetMapping("/orders/{id}")
    17     public String getOrder(@PathVariable(value = "id")Integer id,
    18                            @RequestParam(value = "name")String name,
    19                            @RequestParam(value = "price",required = false,defaultValue = "0") Integer price){
    20         String result = "id:"+id+",name:"+name+",price:"+price;
    21         return result;
    22     }
    23 
    24     /**
    25      * Post使用@RequestBody注解将Json格式的参数自动绑定到Entity类
    26      * @param order
    27      * @return
    28      */
    29     @PostMapping("/order/check")
    30     public String checkOrder(@RequestBody Order order){
    31         String result = "id:"+order.getId()+",name:"+order.getName()+",price:"+order.getPrice();
    32         return result;
    33     }
    34 
    35     /**
    36      * Post使用@RequestParam获取请求体中非Json格式的数据
    37      * @param amount
    38      * @param discount
    39      * @return
    40      */
    41     @PostMapping("/order/checkmore")
    42     public String checkMore(@RequestParam(value = "amount")Integer amount, @RequestParam(value = "discount")float discount){
    43         String result = "amount:"+amount+",discount:"+discount;
    44         return result;
    45     }
    46 
    47     /**
    48      * Post请求也可以直接与对象类绑定,但需要参数名一致,不支持json格式,只支持form-data和x-www.form-urlencoded格式
    49      * @param order
    50      * @return
    51      */
    52     @PostMapping("/order/add")
    53     public String addOrder(Order order){
    54         String result = "id:"+order.getId()+",name:"+order.getName()+",price:"+order.getPrice();
    55         return result;
    56     }
    57 
    58     /**
    59      * Put请求可以直接与对象类绑定,但需要参数名一致
    60      * @param id
    61      * @param order
    62      * @return
    63      */
    64     @PutMapping("/order/{id}/update")
    65     public String updateOrder(@PathVariable(value = "id")Integer id,Order order){
    66         String result = "pathid:"+id+"===Order(id:"+order.getId()+",name:"+order.getName()+",price:"+order.getPrice()+")";
    67         return result;
    68     }
    69 
    70 
    71 }

    注意点:

    1.针对一些非必填的参数,可以使用required关键字来标识,同时必须设置默认值defaultValue,如getOrder方法中对price参数的获取:

      @RequestParam(value = "price",required = false,defaultValue = "0") Integer price

    2.参数可以直接与Entity类绑定,但不支持json格式,只支持form-data和x-www.form-urlencoded格式

      @PostMapping("/order/add")

      public String addOrder(Order order){

    3.使用的Postman做的测试,所有接口都测试通过,也推荐大家使用Postman作为日常的接口测试工具,安装和操作都很简单。

     附部分截图:

        Get:@PathVariable,@RequestParam

      Post:@RequestBody

    获取到参数以后就是要对数据做校验了,在下一篇中进行介绍

  • 相关阅读:
    pat 甲级 1065. A+B and C (64bit) (20)
    pat 甲级 1064. Complete Binary Search Tree (30)
    pat 甲级 1010. Radix (25)
    pat 甲级 1009. Product of Polynomials (25)
    pat 甲级 1056. Mice and Rice (25)
    pat 甲级 1078. Hashing (25)
    pat 甲级 1080. Graduate Admission (30)
    pat 甲级 团体天梯 L3-004. 肿瘤诊断
    pat 甲级 1099. Build A Binary Search Tree (30)
    Codeforce 672B. Different is Good
  • 原文地址:https://www.cnblogs.com/anywherego/p/9600871.html
Copyright © 2020-2023  润新知