SpringBoot:请求参数与基本注解
注解
@PathVariable
@PathVariable:路径变量 (可以放到map中)
@RequestHeader:获取请求头(可以放到map中)
@ModelAttribute:获取request域属性
@RequestParam:获取请求参数(可以放到map中)
@MatrixVariable:矩阵变量
@CookieValue:获取cookie值
@RequestBody获取请求体
@RestController
public class ParameterTestController {
// http://127.0.0.1:8080/car/2/owner/zhangsan?age=15&inters=&inters=
@GetMapping("/car/{id}/owner/{username}")
public Map<String,Object> getCar(@PathVariable("id") Integer id,
@PathVariable("username") String name,
@PathVariable Map<String,String> pv,
@RequestHeader("User-Agent") String userAgent,
@RequestHeader Map<String,String> header,
@RequestParam("age") Integer age,
@RequestParam("inters") List<String> inters,
@RequestParam Map<String,String> params,
@CookieValue("_ga") String _ga,
@CookieValue("_ga") Cookie cookie){
Map<String,Object> map = new HashMap<>();
map.put("id",id);
map.put("name",name);
map.put("pv",pv);
map.put("userAgent",userAgent);
map.put("headers",header);
map.put("age",age);
map.put("inters",inters);
map.put("params",params);
=========equestBody@R获取post请求的请求体===========
@PostMapping("/save")
public Map postMethod(@RequestBody String content){
Map<String,Object> map = new HashMap<>();
map.put("content",content);
return map;
}
==========@RequestAttribute获取request域中============
@RequestMapping("/goto")
public String goTo(HttpServletRequest request){
request.setAttribute("msg","哈哈哈");
return "forward:/success"; //转发扫success请求
}
@ResponseBody
@RequestMapping("/success")
public Map success(@RequestAttribute("msg") String msg,
HttpServletRequest request){
Object msg1 = request.getAttribute("msg");
String msg2 = msg;
Map<String,Object> map = new HashMap<>();
map.put("request_msg",msg1);
map.put("Param",msg2);
return map;
}
map.put("_ga",_ga);
System.out.println(cookie.getName()+"===>"+cookie.getValue());
return map;
}
==============@MatrixVariable===================
//1、语法: 请求路径:/cars/sell;low=34;brand=byd,audi,yd
//2、SpringBoot默认是禁用了矩阵变量的功能
// 手动开启:原理。对于路径的处理。UrlPathHelper进行解析。
// removeSemicolonContent(移除分号内容)支持矩阵变量的
//3、矩阵变量必须有url路径变量才能被解析
@GetMapping("/cars/{path}")
public Map carsSell(@MatrixVariable("low") Integer low,
@MatrixVariable("brand") List<String> brand,
@PathVariable("path") String path){
Map<String,Object> map = new HashMap<>();
map.put("low",low);
map.put("brand",brand);
map.put("path",path);
return map;
}
// /boss/1;age=20/2;age=10
@GetMapping("/boss/{bossId}/{empId}")
public Map boss(@MatrixVariable(value = "age",pathVar = "bossId") Integer bossAge,
@MatrixVariable(value = "age",pathVar = "empId") Integer empAge){
Map<String,Object> map = new HashMap<>();
map.put("bossAge",bossAge);
map.put("empAge",empAge);
return map;
}
}
Servlet Api
阅读剩余
版权声明:
作者:Tin
链接:http://www.tinstu.com/1609.html
文章版权归作者所有,未经允许请勿转载。
THE END