1、Get请求
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); }
}
-
参数用 @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 方法形参接收参数
-
前后端参数名称必须保持一致
@RestController
@RequestMapping(“/user”)
@Slf4j
public class DemoController {@PostMapping("/save") public void saveStudent(String name,String age) {log.info("name:{}",name);log.info("age:{}",age); }
}
-
参数用 @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 注解接收参数
-
接受实体类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;
} -
接收数组或者集合类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);}
}