springboot2-参数、注解和返回值参数注解
@PathVariable:请求路径,从路径获取参数
@ResponseBody
@RequestMapping("/id/{id}/owner/{uname}")
public Map<String, Object>getSome(//@PathVariable("id")int id,
//@PathVariable("uname")String name,
@PathVariable Map<String,String> pv){
Map<String, Object> map =new HashMap<>();
//map.put("id",id);
//map.put("uname",name);
map.put("pv",pv);
return map;
}
使⽤Map<String,String> pv会获取所有的参数的Key和value。
@RequestHeader
@ModelAttribute
@RequestParam:获取请求路径的参数,以?分割后的参数
@ResponseBody
@RequestMapping("/id/{id}/owner/{uname}")
public Map<String, Object>getSome(//@PathVariable("id")int id,
//@PathVariable("uname")String name,
@PathVariable Map<String,String> pv,
@RequestParam("age")int age,
@RequestParam("inters")List<String> inters,
@RequestParam Map<String,String> params){
Map<String, Object> map =new HashMap<>();
//map.put("id",id);
//map.put("uname",name);
//        map.put("pv",pv);
//        System.out.("id"));
map.put("age",age);
map.put("inters",inters);
map.put("params", params);
System.out.("age"));
return map;
}
el表达式获取map的值
也可以⽤Map<String,String> params获取所有的参数。
@MatrixVariable:矩阵变量,根据RFC3986规范,矩阵变量应当绑定在路径变量中,例
如/cars/apth;low=34;brand=byd,bwm,/cars/apth是请求路径,;后⾯的是矩阵变量
@CookieValue
@RequestBody:获取请求体
@ResponseBody
@RequestMapping("/save")
public Map postTest(@RequestBody String body){
Map<String, Object> map =new HashMap<>();
System.out.println(body);
map.put("body",body);
return map;
}
@RequestAttribute:获取request域的属性
@RequestMapping("/gotoPage")
public String gotoPage(HttpServletRequest request){
request.setAttribute("msg","Page");
request.setAttribute("code",200);
return"forward:success";
}
//模拟⽤页⾯的EL表达式获取
//⽤注解或这直接⽤servlet原⽣接⼝获取都⾏
@ResponseBody
@RequestMapping("/success")
public Map attributeTest(@RequestAttribute("msg")String msg,
@RequestAttribute("code")Integer code,
HttpServletRequest request){
System.out.Attribute("msg"));
HashMap<String, Object> map =new HashMap<>();
map.put("req_msg",msg);
map.put("req_code",code);
return map;
}
Servlet API
WebRequest
ServletRequest
MultipartRequest
HttpSession
Principal
InputStream
Reader
HttpMethod
Locale
TimeZone
Zoneld
@ResponseBody
@RequestMapping("/req")
//这三个都是给request域中放数据
public Map requestTest(Map<String,Object> map, Model model, HttpServletRequest request){
return new HashMap();
}
复杂参数
Map
Model
RedirectAttributes
ServletResponse
SessionStatus
UriCompontsBuilder
⾃定义对象参数:⾃动绑定封装
由ServletModelAttributeMethodProcessor处理
⾃定义对象返回:将对象写为json
由RequestResponseBodyMethodProcessor处理
创建⼀个空的⾃定义对象,把http的数据转换成该对象对应的属性的类型,然后绑定到JavaBean中

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。