1. 程式人生 > 其它 >查缺補漏!學習SpringBoot:通過一個請求到底可以獲得多少引數

查缺補漏!學習SpringBoot:通過一個請求到底可以獲得多少引數

查缺補漏!學習SpringBoot:通過一個請求到底可以獲得多少引數

@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


**2.不指定引數名**

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}

## @MatrixVariable註解

如果要使用該註解需要開啟配置

@Component
public class GlobalWebMvcConfigurer implements WebMvcConfigurer {

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
    UrlPathHelper urlPathHelper=new UrlPathHelper();
    urlPathHelper.setRemoveSemicolonContent(false);
    configurer.setUrlPathHelper(urlPathHelper);
}

}


該註解主要用法有兩種,示例如下

**指定引數名**

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}

## @RequestBody註解

post 請求,將請具體封裝成實體bean

post請求體
{
"name":"liu",
"id":"123"
}

@PostMapping("hello")
public String hello(@RequestBody User user){
    System.out.println(user);
    return "hello";
}

user(id=123, name=liu)

## @RequestHeader註解

獲取請求頭的欄位,同樣的有獲取單個請求頭和獲取全部請求頭的兩種用法。


# 最後

每年轉戰網際網路行業的人很多,說白了也是衝著高薪去的,不管你是即將步入這個行業還是想轉行,學習是必不可少的。作為一個Java開發,學習成了日常生活的一部分,不學習你就會被這個行業淘汰,這也是這個行業殘酷的現實。

如果你對Java感興趣,想要轉行改變自己,那就要趁著機遇行動起來。或許,這份**限量版的Java零基礎寶典**能夠對你有所幫助。

領取這份**Java零基礎寶典**,**[只需要點選這裡即可免費下載](https://gitee.com/vip204888/java-p7)**

![](https://upload-images.jianshu.io/upload_images/22932333-7f13b76538e16344?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![](https://upload-images.jianshu.io/upload_images/22932333-48d8fbce839f757d?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)