SpringBoot中Get请求和POST请求接收参数详解

1、Get请求

1.1 方法形参接收参数

  1. 这种方式一般适用参数比较少的情况,并且前后端参数名称必须保持一致

    @RestController

    @RequestMapping("/user")

    @Slf4j

    public class DemoController {

    复制代码
    @GetMapping("/query")
    public void getStudent(String name,String age) {
        log.info("name:{}",name);
        log.info("age:{}",age);
    }

    }

  2. 参数用 @RequestParam 标注,使用value属性指定参数名,required属性表示这个参数是否必传

    @RestController

    @RequestMapping("/user")

    @Slf4j

    public class DemoController {

    复制代码
    @GetMapping("/query")
    public void getStudent(@RequestParam(value = "name", required = false) String name,
                           @RequestParam(value = "age", required = false) String age) {
        log.info("name:{}",name);
        log.info("age:{}",age);
    }

    }

1.2 实体类接收参数

注意:Get 请求以实体类接收参数时,不能用 RequestParam 注解进行标注,因为不支持这样的方式获取参数。

复制代码
@RestController
@RequestMapping("/demo")
@Slf4j
public class DemoController {

    @GetMapping("/query")
    public void getStudent(Student student) {
        log.info("name:{}",student.getName());
        log.info("age:{}",student.getAge());
    }
}

@Data
class Student{
    private String name;
    private Integer age;
}

1.3 通过HttpServletRequest接收参数

复制代码
@RestController
@RequestMapping("/demo")
@Slf4j
public class DemoController {

    @GetMapping("/query")
    public void getStudent(HttpServletRequest request) {
        String name = request.getParameter("name");
        String phone = request.getParameter("age");
        log.info("name:{}",name);
        log.info("age:{}",age);
    }
}

1.4 通过@PathVariable接收参数

复制代码
@RestController
@RequestMapping("/demo")
@Slf4j
public class DemoController {

    @GetMapping("/query/{name}/{age}")
    public void getStudent(@PathVariable String name, @PathVariable String age) {
        log.info("name:{}",name);
        log.info("age:{}",age);
    }
}

1.5 接收数组参数

复制代码
@RestController
@RequestMapping("/demo")
@Slf4j
public class DemoController {

    @GetMapping("/query")
    public void getStudent(String[] names) {
        Arrays.stream(names).forEach(System.out::println);
    }
}

1.6 接受集合参数

注意:SpringBoot 接收集合参数,必须用 @RequestParam 注解声明!

复制代码
@RestController
@RequestMapping("/demo")
@Slf4j
public class DemoController {

    @GetMapping("/query")
    public void getStudent(@RequestParam List<String> names) {
        names.forEach(System.out::println);
    }
}

2、POST请求

2.1 方法形参接收参数

  1. 前后端参数名称必须保持一致

    @RestController

    @RequestMapping("/user")

    @Slf4j

    public class DemoController {

    复制代码
    @PostMapping("/save")
    public void saveStudent(String name,String age) {
        log.info("name:{}",name);
        log.info("age:{}",age);
    }

    }

  2. 参数用 @RequestParam 标注,使用value属性指定参数名,required属性表示这个参数是否必传

    @RestController

    @RequestMapping("/user")

    @Slf4j

    public class DemoController {

    复制代码
    @PostMapping("/save")
    public void saveStudent(@RequestParam(value = "name", required = false) String name,
                           @RequestParam(value = "age", required = false) String age) {
        log.info("name:{}",name);
        log.info("age:{}",age);
    }

    }

2.2 通过HttpServletRequest接收参数

复制代码
@RestController
@RequestMapping("/demo")
@Slf4j
public class DemoController {

    @PostMapping("/save")
    public void saveStudent(HttpServletRequest request) {
        String name = request.getParameter("name");
        String phone = request.getParameter("age");
        log.info("name:{}",name);
        log.info("age:{}",age);
    }
}

2.3 通过@PathVariable接收参数

复制代码
@RestController
@RequestMapping("/demo")
@Slf4j
public class DemoController {

    @PostMapping("/save/{name}/{age}")
    public void saveStudent(@PathVariable String name, @PathVariable String age) {
        log.info("name:{}",name);
        log.info("age:{}",age);
    }
}

2.4 通过param方式提交参数,以实体类接收参数

直接以实体类可以接收param、form-data、 x-www-form-urlencoded 提交的参数

复制代码
@RestController
@RequestMapping("/demo")
@Slf4j
public class DemoController {

    @PostMapping("/save")
    public void saveStudent(Student student) {
        log.info("name:{}",student.getName());
        log.info("age:{}",student.getAge());
    }
}

@Data
class Student{
    private String name;
    private Integer age;
}

2.5 请求体以JSON格式提交参数,通过 @RequestBody 注解接收参数

  1. 接受实体类JSON参数

    @RestController

    @RequestMapping("/demo")

    @Slf4j

    public class DemoController {

    复制代码
    @PostMapping("/save")
    public void saveStudent(@RequestBody Student student) {
        log.info("name:{}",student.getName());
        log.info("age:{}",student.getAge());
    }

    }

    @Data

    class Student{

    private String name;

    private Integer age;

    }

  2. 接收数组或者集合类JSON参数

    @RestController

    @RequestMapping("/demo")

    @Slf4j

    public class DemoController {

    复制代码
    @PostMapping("/save")
    public void saveStudent(@RequestBody String[] names) {
        Arrays.stream(names).forEach(System.out::println);
    }
    
    @PostMapping("/save")
    public void saveStudent(@RequestBody List<String> names) {
        names.forEach(System.out::println);
    }

    }

2.6 通过 Map 接收参数

复制代码
@RestController
@RequestMapping("/demo")
@Slf4j
public class DemoController {

    @PostMapping("/save")
    public void saveStudent(@RequestParam Map<String,Object> map) {
        log.info("name:{}",map.get("name"));
        log.info("age:{}",map.get("age"));
    }
}

2.7 通过@RequestBody 接收一个参数

复制代码
@RestController
@RequestMapping("/demo")
@Slf4j
public class DemoController {

    @PostMapping("/save")
    public void saveStudent(@RequestBody String name) {
        log.info("name:{}",name);
    }
}
相关推荐
bbq粉刷匠40 分钟前
java刷题-day1
java·开发语言
讓丄帝愛伱1 小时前
excel导出实例
java·python·excel
g***86691 小时前
springboot中配置logback-spring.xml
spring boot·spring·logback
p***q781 小时前
SpringBoot实战:高效实现API限流策略
java·spring boot·后端
3***16101 小时前
【JavaEE】Spring Boot 项目创建
java·spring boot·java-ee
6***v4171 小时前
VScode 开发 Springboot 程序
java·spring boot·后端
t***31651 小时前
SpringBoot中自定义Starter
java·spring boot·后端
橘子编程1 小时前
经典排序算法全解析
java·算法·排序算法
z***3351 小时前
SpringBoot获取bean的几种方式
java·spring boot·后端