一,简单参数
- Get请求:只需要在postman中的Params参数与方法中的形参一致就可以
- Post请求与Get方法一致只需要在
如果参数名不一致--通过@RequestParam中的value属性执行请求参数名
- @RequestParam(name = "name",required =false) //表示name参数不是必须传递
@RequestMapping("/simpleParam") public String simpleParam(@RequestParam("name") String username , Integer age ){System.out.println(username+" : "+age);return "OK"; }
- 如果400表示传递的参数有问题
二,传递实体参数(实体类)
1,简单实体参数(无嵌套)
- 我们可以考虑将请求参数封装到一个实体类对象中。 要想完成数据封装,需要遵守如下规则:请求参数名与实体类的属性名相同
@RestController public class RequestController {//实体参数:简单实体对象@RequestMapping("/simplePojo")public String simplePojo(User user){System.out.println(user);return "OK";} }
当参数名和实体类属性不一致时该属性无法自动接收会显示NULL
2,复杂实体类参数(有嵌套)
复杂实体对象的封装,需要遵守如下规则:
请求参数名与形参对象属性名相同,按照对象层次结构关系即可接收嵌套实体类属性参数。- 生成GET和SET 和toString方法
public class Address {private String province;private String city;public String getProvince() {return province;}public void setProvince(String province) {this.province = province;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}@Overridepublic String toString() {return "Address{" +"province='" + province + '\'' +", city='" + city + '\'' +'}';} }
public class User {private String name;private Integer age;private Address address; //地址对象public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +", age=" + age +", address=" + address +'}';} }
三, 数组参数提交
1,前端参数传递
@RestController public class RequestController {//数组集合参数@RequestMapping("/arrayParam")public String arrayParam(String[] hobby){System.out.println(Arrays.toString(hobby));return "OK";} }
四,集合参数提交
1, 前端参数与数组传递方式一样,主要是后端不一样
@RestController public class RequestController {//数组集合参数@RequestMapping("/listParam")public String listParam(@RequestParam List<String> hobby){System.out.println(hobby);return "OK";} }
四,JSON参数提交
封装规则:JSON数据键名与形参对象属性名相同,定义POJO类型形参即可接收参数。需要使用 @RequestBody标识。
实体类上要有GET和SET方法
五,路径参数提交
1,适用于根据ID查询数据
@RestController public class RequestController {//路径参数@RequestMapping("/path/{id}")public String pathParam(@PathVariable Integer id){System.out.println(id);return "OK";} }
2,传递多个路径参数
3,根据ID删除多个参数
@DeleteMapping("/{ids}")public Result delete(@PathVariable List<Integer> ids){log.info("批量删除操作, ids:{}",ids);classService.delete(ids);return Result.success();}
六,日期参数
@RestController public class RequestController {//日期时间参数@RequestMapping("/dateParam")public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime){System.out.println(updateTime);return "OK";} }
七,响应
controller中自动集成了响应参数
@RestController中自动集成了@RequestMapping和@ResponseBody
@ResponseBody注解会将数据返回给请求数据的页面
@RestController public class HelloController {@RequestMapping("/hello")public String hello(){System.out.println("Hello World ~");return "Hello World ~";} }