web第六次课后作业--使用ApiFox实现请求响应操作
一、实体参数
1.1 简单实体参数
1.2 复杂实体对象
- 如果请求参数比较多,通过上述的方式一个参数一个参数的接收会比较繁琐。
- 此时,我们可以考虑将请求参数封装到一个实体类对象中。 要想完成数据封装,需要遵守如下规则:请求参数名与实体类的属性名相同。
- 在实体类中可以有一个或多个属性,也可以是实体对象类型的。如:
User类中有一个Address类型的属性(Address是一个实体类)
二、数组集合参数
2.1 数组参数
- 请求参数名与形参数组名称相同且请求参数为多个,定义
数组类型形参
即可接收参数。
@RestController
public class RequestController {//数组集合参数@RequestMapping("/arrayParam")public String arrayParam(String[] hobby){System.out.println(Arrays.toString(hobby));return "OK";}
}
- 前端传递的两种方式:
- http://localhost:8080/xxxxxxxxxx?hobby=game&hobby=java
2.http://localhost:8080/xxxxxxxxxxxxx?hobby=game,java
- http://localhost:8080/xxxxxxxxxx?hobby=game&hobby=java
- 运行结果
2.2 集合参数
- 默认情况下,请求中参数名相同的多个值,是封装到数组。如果要封装到集合,要使用@RequestParam绑定参数关系。
@RestController
public class RequestController {//数组集合参数@RequestMapping("/listParam")public String listParam(@RequestParam List<String> hobby){System.out.println(hobby);return "OK";}
}
三、日期参数
- @DateTimeFormat注解的pattern属性中指定了哪种日期格式,前端的日期参数就必须按照指定的格式传递。
- 后端controller方法中,需要使用Date类型或LocalDateTime类型,来封装传递的参数。
@RestController
public class RequestController {//日期时间参数@RequestMapping("/dateParam")public String dateParam(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime) {DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");System.out.println(updateTime.format(formatter)); // 输出格式化后的字符串return "OK";}
}
运行结果