注解类型:
-
@PathVariable :路径变量
-
@RequestHeader:获取请求头
-
@RequestParam :获取请求参数
-
@CookieValue:获取cookie的值
-
@RequestBody : 获取请求体[post]
-
@RequestAttribute 获取request域属性
-
@RequestAttribute 获取request域属性
-
@MatrixVariable 矩阵变量
详细说明
- @PathVariable :路径变量
- @RequestHeader:获取请求头
- @RequestParam :获取请求参数
- @CookieValue:获取cookie的值
<a href="param/1/name/2?age=28&insterting=baseball">PathVariable</a>@RestController public class PamaretertController { @GetMapping(value = "/param/{id}/name/{name}") public Map<String,Object> get(@PathVariable("id") String id, @PathVariable("name") String name, @PathVariable Map<String,String> pv, @RequestHeader("User-Agent") String userAgent, @RequestHeader Map<String,String> header, @RequestParam("age") Integer age, @RequestParam("insterting")List<String> ins, @RequestParam Map<String,String> params, @CookieValue("_ga") Cookie cookie ){ Map<String,Object> map1 = new HashMap<>(); // map1.put("id",id); // map1.put("name",name); // map1.put("pv",pv); // map1.put("header",header); // map1.put("userAgent",userAgent); // map1.put("age",age); // map1.put("ins",ins); // map1.put("params",params); map1.put("cookie",cookie); return map1; }}
- @RequestBody : 获取请求体[post]
<form action="/user/save" method="post"> 用户名:<input type="text" name="name"> </br> 邮箱:<input type="text" name="email"> <button type="submit">提交</button> </form>@PostMapping(value = "/user/save") public Map<String,Object> save(@RequestBody String content){ Map<String,Object> map = new HashMap<>(); map.put("content",content); return map; }
- @RequestAttribute 获取request域属性
@Controller public class RequestController { @GetMapping(value = "/goto") public String gotoPage(HttpServletRequest request){ request.setAttribute("msg","成功了"); request.setAttribute("code",100); return "forward:success"; } @ResponseBody @GetMapping(value ="/success") public Map success(@RequestAttribute("msg") String msg, @RequestAttribute("code") Integer code, HttpServletRequest request){ Map<String,Object> map = new HashMap(); map.put("msg",msg); map.put("code",code); map.put("request_msg",request.getAttribute("msg")); return map; } }
- @MatrixVariable 矩阵变量
springboot 默认是禁止矩阵变量功能,手动开启原理:
两种方法: urlPathHelper.setRemoveSemicolonContent(false);
1、实现WebMvcConfigurer
@Configuration
public class Myconfig implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}
2、自定义WebMvcConfigurer
@Bean
public WebMvcConfigurer getWebMvcConfigurer(){
return new WebMvcConfigurer() {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
//不移除后面分号的内容,矩阵变量功能可以生效
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
};
}
使用:
<a href="/car/sell;low=34;brand=byd,audi,yd">@MatrixVariable</a> <a href="/car/sell;low=34;brand=byd;brand=audi;brand=yd">@MatrixVariable</a> <a href="/boss/1;age=20/2;age=10">@MatrixVariable</a>
例子1
@GetMapping(value = "/car/{path}")
public Map<String,Object> car(@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;
}
例2
@GetMapping(value = "/boss/{bossId}/{empId}")
public Map<String,Object> 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;
}
本文介绍SpringBoot中RESTful API如何通过多种注解处理HTTP请求中的不同类型的参数,包括路径变量、请求头、请求参数、Cookie值、请求体等。
8万+

被折叠的 条评论
为什么被折叠?



