SpringBoot如何獲取Get請求參數(shù)詳解
前言
利用 Spring Boot 來制作 Web 應用,就必定會涉及到前端與后臺之間互相傳遞參數(shù)。下面演示 Controller 如何接收以 GET 方式傳遞過來的參數(shù)。
一、直接在請求路徑中
(1)、假設請求地址是如下這種 RESTful 風格,Springboot 這個參數(shù)值直接放在路徑里面
http://localhost:8080/hello/Springboot
(2)、Controller 定義,如下:
@RestController public class HelloController { @GetMapping("/hello/{name}") public String hello(@PathVariable("name") String name){ return "你好," + name + " !"; } }
二、參數(shù)跟在 ? 號后面
1,獲取參數(shù)的基本方法
(1)假設請求地址是如下這種傳統(tǒng)方式,參數(shù)跟在問號后面:
http://localhost:8080/hello?name=Springboot
(2)Controller 定義如下所示:
@RestController public class HelloController { @GetMapping("/hello") public String hello(@RequestParam("name") String name){ return "你好," + name + " !"; } }
可以把在注解 @RequestParam 添加 required = false 設置參數(shù)為非必輸項
可以把在注解 @RequestParam 添加 defaultValue 參數(shù) 設置默認值。
2、使用 map 來接收參數(shù)
(1)Controller 還可以直接使用 map 來接收所有的請求參數(shù):
@RestController public class HelloController { @GetMapping("/hello") public String hello(@RequestParam Map<String, Object> map){ return "圖書: " + map.get("name") + " 的作者為: " + map.get("author"); } }
(2)、結果如下所示:
3、接收一個集合
(1)、Controller 定義接收 數(shù)組或者List的方法,如下:
@RestController public class HelloController { @GetMapping("/array") public String array(@RequestParam("name") String[] names){ System.out.println("我是數(shù)組接收的參數(shù):" + Arrays.toString(names)); return "我是數(shù)組接收的參數(shù):" + Arrays.toString(names); } @GetMapping("/list") public String list(@RequestParam("name")List<String> names){ System.out.println("我是List接收的參數(shù):" + names); return "我是List接收的參數(shù):" + names.toString(); } }
(2)、測試結果
4、通過對象接收參數(shù)
如果一個 get 請求的參數(shù)太多,我們構造一個對象來簡化參數(shù)的接收方式。
(1)、定義一個基本對象Book 和 Controller
public class Book { private String name; private String author; private Double price; // 省略 getter setter 方法 }
@RestController public class BookController { @GetMapping("/book") public Book book(Book book){ System.out.println("接收到的參數(shù)為:" + book); return book; } }
(2)、基本用法:
(3)、構造多個對象來接收參數(shù),代碼如下所示:
public class User { private String username; private String password; // 省略 getter setter 方法 }
@RestController public class BookController { @GetMapping("/book") public Book book(Book book, User user){ System.out.println("接收到的參數(shù)為:" + book); System.out.println("接收到的參數(shù)為:" + user); return book; } }
訪問:
輸出結果:
(4)、指定前綴。
如果在多個對象參數(shù)的方法中,對象之間存在相同的 屬性時 可以結合 @InitBinder 指定前綴的方式處理。如下所示,Book 對象和 Author 對象中都存在 name 屬性。
除了在 Controller 里單獨定義預處理方法外,我們還可以通過 @ControllerAdvice 結合 @InitBinder 來定義全局的參數(shù)預處理方法,方便各個 Controller 使用
public class Book { private String name; private String author; } public class Author { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Author{" + "name='" + name + '\'' + '}'; } }
@RestController public class BookController { @GetMapping("/book") public Book book(@ModelAttribute("b") Book book, @ModelAttribute("a") Author author){ System.out.println("接收到的參數(shù)為:" + book); System.out.println("接收到的參數(shù)為:" + author); return book; } // 添加 InitBinder 指定 前綴 b @InitBinder("b") private void initBinderB(WebDataBinder binder) { binder.setFieldDefaultPrefix("b."); } // 添加 InitBinder 指定 前綴 a @InitBinder("a") private void initBinderA(WebDataBinder binder) { binder.setFieldDefaultPrefix("a."); } }
訪問:
結果:
總結
到此這篇關于SpringBoot如何獲取Get請求參數(shù)的文章就介紹到這了,更多相關SpringBoot獲取Get請求參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Springboot GET和POST請求參數(shù)獲取方式小結
- Springboot中攔截GET請求獲取請求參數(shù)驗證合法性核心方法
- springboot如何接收get和post請求參數(shù)
- springboot中Getmapping獲取參數(shù)的實現(xiàn)方式
- SpringBoot用實體接收Get請求傳遞過來的多個參數(shù)的兩種方式
- SpringBoot常見get/post請求參數(shù)處理、參數(shù)注解校驗及參數(shù)自定義注解校驗詳解
- 解決Springboot get請求是參數(shù)過長的情況
- Springboot接收Get參數(shù)實踐過程
相關文章
mybatis?resultMap沒有全部對應的字段處理方式
這篇文章主要介紹了mybatis?resultMap沒有全部對應的字段處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03JAVA 生成隨機數(shù)并根據(jù)后臺概率靈活生成的實例代碼
本篇文章主要介紹了JAVA 生成隨機數(shù)并根據(jù)后臺概率靈活生成的實例代碼,具有一定的參考價值,有興趣的可以了解一下2017-08-08arthas?jprofiler做復雜鏈路的調(diào)用分析
這篇文章主要為大家介紹了arthas?jprofiler做復雜鏈路的調(diào)用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06jackson 實現(xiàn)null轉0 以及0轉null的示例代碼
這篇文章主要介紹了jackson 實現(xiàn)null轉0 以及0轉null的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09使用kotlin集成springboot開發(fā)的超詳細教程
目前大多數(shù)都在使用java集成 springboot進行開發(fā),本文演示僅僅將 java換成 kotlin,其他不變的情況下進行開發(fā),需要的朋友可以參考下2021-09-09