SpringBoot學習之-經過一個請求能夠得到多少參數

以前在SpringBoot源碼解析-controller層參數的封裝 中已經分析過springboot中controller層參數封裝的原理,可是工做中畢竟不會一直有時間給你慢慢分析,有時候快速查詢也是很必要的。因此今天就總結一下controller層哪些參數封裝註解的含義,以及使用,方便之後快速查閱。spring

@RequestParam註解

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

1.指定參數名

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
複製代碼

2.不指定參數名

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

@PathVariable註解

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

1.指定參數名

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
複製代碼

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註解

若是要使用該註解須要開啓配置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}
複製代碼

@RequestBody註解

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)
複製代碼

@RequestHeader註解

獲取請求頭的字段,一樣的有獲取單個請求頭和獲取所有請求頭的兩種用法。post

@CookieValue註解

獲取cookie中的鍵值對的值。ui

請求頭添加 Cookie:value=liuyu
    @GetMapping("hello")
    public String hello(@CookieValue(name = "value") String user){
        System.out.println(user);
        return "hello";
    }
    user = liuyu
複製代碼

@SessionAttribute註解 和 @RequestAttribute註解

這兩個註解功能有點像,一個實在session中尋找相應的對象,一個是在request中找。url


返回目錄

相關文章
相關標籤/搜索