002 SpringBoot 常用注解
Controller 接收参数
@PathVariable
获取路径参数@GetMapping("/{depid}/{userid}")
func(@PathVariable("depid") int dep_id){}
func(@PathVariable("userid") int user_id){}
@RequestParam
获取查询参数http://www.baidu.com?name=张三&age=18
func(@RequestParam("name") String name){}
func(@RequestParam("age") int age){}
func(@RequestParam("head_img") MultipartFile file){}
图片上传
@RequestBody
"{name:'张三', age:20}"
func(@RequestBody Map<String, String> person){}
System.out.println(person.get("name"));
@RequestHeader
获取请求头@CookieValue
获取 COOKIEHttpServletRequest
func(HttpServletRequest request){ String name = request.getParameter("name"); }
异常处理
@ControllerAdvice
@RestControllerAdvice
如果是返回 JSON 数据,则用这个,就可以不加@ResponseBody
@ExceptionHandler(value=Exception.class)
捕获全局异常,处理所有不可知的异常
本作品采用《CC 协议》,转载必须注明作者和本文链接
....是挺常用的 :sweat_smile: