一、请求
1.简单参数
- 原始方式:
在原始的web程序中,获取请求参数,需要通过HttpServletRequest对象手动获取
@RequestMapping("/simpleParam")public String simpleParam(HttpServletRequest request){String name = request.getParameter("name");String ageStr = request.getParameter("age");int age = Integer.parseInt(ageStr);System.out.println(name + age);return "OK";}
- SpringBoot方式
简单参数:参数名与形参变量名相同,定义形参即可接收参数
//Springboot方式@RequestMapping("/simpleParam")public String simpleParam(String name,Integer age){System.out.println(name + age);return "OK";}
如果方法形参名与请求参数名称不匹配,可以使用@RequestParam完成映射
注意事项:@RequestParam中的required属性默认为true,代表该请求参数必须传递,如果不传递将报错。该参数是可选的,可以设置为false
@RequestMapping("/simpleParam")public String simpleParam(@RequestParam(name = "name") String username, Integer age){System.out.println(username + age);return "OK";}
2.实体参数
- 简单实体对象:请求参数名与形参对象属性名相同,定义POJO接收即可
3.数组集合参数
- 数组参数:请求参数名与形参数组名称相同且请求参数为多个,定义数组类型形参即可接收参数
@RequestMapping("/arrayParam")public String arrayParam(String[] hobby){System.out.println(Arrays.toString(hobby));return "OK";}
- 数组集合参数:通过注解RequestParam绑定参数
@RequestMapping("/listParam")public String listParam(@RequestParam List<String> hobby){System.out.println(hobby);return "OK";}
4.日期参数
- 使用@DateTimeFormat注解完成日期参数格式转换
@RequestMapping("/dateParam")public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime updateTime){System.out.println(updateTime);return "OK";}
5.Json参数
- Json数据键名与形参对象属性名相同,定义POJO类型形参即可接收参数,需要使用@RequestBody标识
@RequestMapping("/jsonParam")public String jsonParam(@RequestBody User user) {System.out.println(user);return "OK";}
6.路径参数
- 通过请求URL直接传递参数,使用{...}来标识该路径参数,需要使用@PathVariable获取路径参数
@RequestMapping("/path/{id}")public String pathParam(@PathVariable Integer id) {System.out.println(id);return "OK";}
获取多个路径参数
@RequestMapping("/path/{id}/{name}")public String pathParam(@PathVariable Integer id,@PathVariable String name) {System.out.println(id);System.out.println(name);return "OK";}
二、响应
1.@ResponseBody
- 类型:方法注解、类注解
- 位置:Controller方法上
- 作用:将方法返回值直接响应,如果返回值类型是实体对象/集合,将会转换为JSON格式响应
- 说明:@RestController=@Controller+@ResponseBody
2.统一响应结果
- 统一响应结果封装类
public class Result {private Integer code; //1 成功 0 失败private String msg; //提示信息private Object data; //数据 datepublic Result(Integer code, String msg, Object data) {this.code = code;this.msg = msg;this.data = data;}public Result() {}public Integer getCode() {return code;}public void setCode(Integer code) {this.code = code;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}public static Result success(Object data){return new Result(1,"success",data);}public static Result success(){return new Result(1,"success",null);}public static Result error(String msg){return new Result(0,msg,null);}@Overridepublic String toString() {return "Result{" +"code=" + code +", msg='" + msg + '\'' +", data=" + data +'}';}
}
@RestController
public class ResponseController {@RequestMapping("/hello")public Result hello(){System.out.println("Hello,world");return Result.success("hello,world");}@RequestMapping("/getAddr")public Result getAddr(){Address addr = new Address();addr.setProvince("广东");addr.setCity("深圳");return Result.success(addr);}@RequestMapping("/listAddr")public Result listAddr(){ArrayList<Object> list = new ArrayList<>();Address addr = new Address();addr.setProvince("广东");addr.setCity("深圳");Address addr2 = new Address();addr2.setProvince("湖南");addr2.setCity("长沙");list.add(addr);list.add(addr2);return Result.success(list);}
}