SpringBoot如何獲取Get請求參數(shù)詳解
前言
利用 Spring Boot 來制作 Web 應(yīng)用,就必定會涉及到前端與后臺之間互相傳遞參數(shù)。下面演示 Controller 如何接收以 GET 方式傳遞過來的參數(shù)。
一、直接在請求路徑中
(1)、假設(shè)請求地址是如下這種 RESTful 風(fēng)格,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)假設(shè)請求地址是如下這種傳統(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è)置參數(shù)為非必輸項
可以把在注解 @RequestParam 添加 defaultValue 參數(shù) 設(shè)置默認(rèn)值。
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)、結(jié)果如下所示:

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)、測試結(jié)果


4、通過對象接收參數(shù)
如果一個 get 請求的參數(shù)太多,我們構(gòu)造一個對象來簡化參數(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)、構(gòu)造多個對象來接收參數(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;
}
}訪問:

輸出結(jié)果:

(4)、指定前綴。
如果在多個對象參數(shù)的方法中,對象之間存在相同的 屬性時 可以結(jié)合 @InitBinder 指定前綴的方式處理。如下所示,Book 對象和 Author 對象中都存在 name 屬性。
除了在 Controller 里單獨(dú)定義預(yù)處理方法外,我們還可以通過 @ControllerAdvice 結(jié)合 @InitBinder 來定義全局的參數(shù)預(yù)處理方法,方便各個 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.");
}
}訪問:

結(jié)果:

總結(jié)
到此這篇關(guān)于SpringBoot如何獲取Get請求參數(shù)的文章就介紹到這了,更多相關(guān)SpringBoot獲取Get請求參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Springboot GET和POST請求參數(shù)獲取方式小結(jié)
- Springboot中攔截GET請求獲取請求參數(shù)驗證合法性核心方法
- springboot如何接收get和post請求參數(shù)
- springboot中Getmapping獲取參數(shù)的實(shí)現(xiàn)方式
- SpringBoot用實(shí)體接收Get請求傳遞過來的多個參數(shù)的兩種方式
- SpringBoot常見get/post請求參數(shù)處理、參數(shù)注解校驗及參數(shù)自定義注解校驗詳解
- 解決Springboot get請求是參數(shù)過長的情況
- Springboot接收Get參數(shù)實(shí)踐過程
相關(guān)文章
mybatis?resultMap沒有全部對應(yīng)的字段處理方式
這篇文章主要介紹了mybatis?resultMap沒有全部對應(yīng)的字段處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
JAVA 生成隨機(jī)數(shù)并根據(jù)后臺概率靈活生成的實(shí)例代碼
本篇文章主要介紹了JAVA 生成隨機(jī)數(shù)并根據(jù)后臺概率靈活生成的實(shí)例代碼,具有一定的參考價值,有興趣的可以了解一下2017-08-08
arthas?jprofiler做復(fù)雜鏈路的調(diào)用分析
這篇文章主要為大家介紹了arthas?jprofiler做復(fù)雜鏈路的調(diào)用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
jackson 實(shí)現(xiàn)null轉(zhuǎn)0 以及0轉(zhuǎn)null的示例代碼
這篇文章主要介紹了jackson 實(shí)現(xiàn)null轉(zhuǎn)0 以及0轉(zhuǎn)null的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
使用kotlin集成springboot開發(fā)的超詳細(xì)教程
目前大多數(shù)都在使用java集成 springboot進(jìn)行開發(fā),本文演示僅僅將 java換成 kotlin,其他不變的情況下進(jìn)行開發(fā),需要的朋友可以參考下2021-09-09

