以前在SpringBoot源碼解析-controller層參數的封裝 中已經分析過springboot中controller層參數封裝的原理,可是工做中畢竟不會一直有時間給你慢慢分析,有時候快速查詢也是很必要的。因此今天就總結一下controller層哪些參數封裝註解的含義,以及使用,方便之後快速查閱。spring
該註解主要用法有兩種,示例以下springboot
http://127.0.0.1:8080/hello?id=123
@GetMapping("hello")
public String hello(@RequestParam("id") String id){
log.info("[{}]",id);
return "hello";
}
id = 123
複製代碼
http://127.0.0.1:8080/hello?id=123&name=liuyu
@GetMapping("hello")
public String hello(@RequestParam Map<String,String> value){
System.out.println(value);
return "hello";
}
value = {id=123, name=liuyu}
複製代碼
若是咱們想獲取所有的get請求值,能夠不明確指定RequestParam註解的value,而且使用map來接收參數。這樣咱們就能夠獲得所有的請求值。bash
該註解主要用法有兩種,示例以下cookie
http://127.0.0.1:8080/hello/liuyu/qwert
@GetMapping("hello/{id}/{name}")
public String hello(@PathVariable("id") String id,@PathVariable("name") String name){
System.out.println(value);
System.out.println(name);
return "hello";
}
id = liuyu ,name = qwert
複製代碼
http://127.0.0.1:8080/hello/liuyu/qwert
@GetMapping("hello/{id}/{name}")
public String hello(@PathVariable Map<String,String> map){
System.out.println(map);
return "hello";
}
map = {id=liuyu, name=qwert}
複製代碼
若是要使用該註解須要開啓配置session
@Component
public class GlobalWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper=new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}
複製代碼
該註解主要用法有兩種,示例以下app
http://127.0.0.1:8080/hello/liu;value=123
@GetMapping("hello/{id}")
public String hello(@PathVariable(name = "id") String name,@MatrixVariable(name = "value") String value){
System.out.println(name);
System.out.println(value);
return "hello";
}
id = liu
value = 123
複製代碼
http://127.0.0.1:8080/hello/liu;value=123;name=qwe
@GetMapping("hello/{id}")
public String hello(@PathVariable(name = "id") String name,@MatrixVariable Map<String,String> value){
System.out.println(name);
System.out.println(value);
return "hello";
}
id = liu
value = {value=123, name=qwe}
複製代碼
post 請求,將請具體封裝成實體beanide
post請求體
{
"name":"liu",
"id":"123"
}
@PostMapping("hello")
public String hello(@RequestBody User user){
System.out.println(user);
return "hello";
}
user(id=123, name=liu)
複製代碼
獲取請求頭的字段,一樣的有獲取單個請求頭和獲取所有請求頭的兩種用法。post
獲取cookie中的鍵值對的值。ui
請求頭添加 Cookie:value=liuyu
@GetMapping("hello")
public String hello(@CookieValue(name = "value") String user){
System.out.println(user);
return "hello";
}
user = liuyu
複製代碼
這兩個註解功能有點像,一個實在session中尋找相應的對象,一個是在request中找。url