Springboot接收Get參數(shù)實踐過程
一、參數(shù)直接在路徑中
1.假設(shè)請求地址是如下這種 RESTful 風(fēng)格
hangge 這個參數(shù)值直接放在路徑里面:
http://localhost:8080/helloworld/張三
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @GetMapping("/helloworld/{name}") public String helloworld(@PathVariable("name") String name) { return "獲取到的name是:" + name; } }
二、參數(shù)跟在 ? 號后面
1.獲取參數(shù)的基本方法
(1)假設(shè)請求地址是如下這種傳統(tǒng)方式,參數(shù)跟在問號后面:
http://localhost:8080/helloworld1?name=張三
(2)Controller 可以這么獲取該參數(shù):
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; @RestController public class HelloController { @GetMapping("/helloworld1") public String helloworld1(@RequestParam("name") String name) { return "獲取到的name是:" + name; } }
2.參數(shù)沒有傳遞的情況
(1)如果沒有傳遞參數(shù) Controller 將會報錯,我們可以使用 required = false 標(biāo)注參數(shù)是非必須的。
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; @RestController public class HelloController { @GetMapping("/helloworld2") public String helloworld2(@RequestParam(name = "name", required = false) String name) { return "獲取到的name是:" + name; } }
(2)或者可以指定個默認(rèn)值,當(dāng)沒有傳遞參數(shù)時自動使用默認(rèn)值:
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; @RestController public class HelloController { @GetMapping("/helloworld3") public String helloworld3(@RequestParam(name = "name", defaultValue = "xxx") String name) { return "獲取到的name是:" + name; } }
3.使用 map 來接收參數(shù)
(1)Controller 還可以直接使用 map 來接收所有的請求參數(shù):
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; import java.util.Map; @RestController public class HelloController { @GetMapping("/helloworld4") public String helloworld4(@RequestParam Map<String, Object> params) { return "name:" + params.get("name") + "<br>age:" + params.get("age"); } }
(2)下面是一個簡單的測試樣例:
4.接收一個數(shù)組
(1)假設(shè)請求地址是如下這種,有多個同名參數(shù):
http://localhost:8080/helloworld5?name=zhangsan&name=lisi
(2)我們可以定義一個數(shù)組類型的參數(shù)來接收:
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; @RestController public class HelloController { @GetMapping("/helloworld5") public String helloworld5(@RequestParam("name") String[] names) { String result = ""; for(String name:names){ result += name + "<br>"; } return result; } }
附:使用對象來接收參數(shù)
1.基本用法
(1)如果一個 get 請求的參數(shù)太多,我們構(gòu)造一個對象來簡化參數(shù)的接收方式:
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; @RestController public class HelloController { @GetMapping("/helloworld6") public String helloworld6(User user) { return "name:" + user.getName() + "<br> age:" + user.getAge(); } }
(2)User 類的定義如下,到時可以直接將多個參數(shù)通過 getter、setter 方法注入到對象中去:
public class User { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
(3)下面是一個簡單的測試樣例:
(4)如果傳遞的參數(shù)有前綴,且前綴與接收實體類的名稱相同,那么參數(shù)也是可以正常傳遞的:
2.指定參數(shù)前綴
(1)如果傳遞的參數(shù)有前綴,且前綴與接收實體類的名稱不同相,那么參數(shù)無法正常傳遞:
(2)我們可以結(jié)合 @InitBinder 解決這個問題,通過參數(shù)預(yù)處理來指定使用的前綴為 u.
除了在 Controller 里單獨定義預(yù)處理方法外,我們還可以通過 @ControllerAdvice 結(jié)合 @InitBinder 來定義全局的參數(shù)預(yù)處理方法,方便各個 Controller 使用。具體做法參考我之前的文章:
- SpringBoot - @ControllerAdvice的使用詳解3(請求參數(shù)預(yù)處理 @InitBinder)
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.*; @RestController public class HelloController { @GetMapping("/helloworld7") public String helloworld7(@ModelAttribute("u") User user) { return "name:" + user.getName() + "<br> age:" + user.getAge(); } @InitBinder("u") private void initBinder(WebDataBinder binder) { binder.setFieldDefaultPrefix("u."); } }
(3)重啟程序可以看到參數(shù)以及成功接收了:
3.構(gòu)造多個對象來接收參數(shù)
(1)如果一個 get 請求的參數(shù)分屬不同的對象,也可以使用多個對象來接收參數(shù):
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; @RestController public class HelloController { @GetMapping("/helloworld8") public String helloworld8(User user, Phone phone) { return "name:" + user.getName() + "<br> age:" + user.getAge() + "<br> number:" + phone.getNumber(); } }
(2)新增的 Phone 類定義如下:
public class Phone { private String number; public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } }
(3)下面是一個簡單的測試樣例:
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Springboot GET和POST請求參數(shù)獲取方式小結(jié)
- Springboot中攔截GET請求獲取請求參數(shù)驗證合法性核心方法
- springboot如何接收get和post請求參數(shù)
- SpringBoot如何獲取Get請求參數(shù)詳解
- springboot中Getmapping獲取參數(shù)的實現(xiàn)方式
- SpringBoot用實體接收Get請求傳遞過來的多個參數(shù)的兩種方式
- SpringBoot常見get/post請求參數(shù)處理、參數(shù)注解校驗及參數(shù)自定義注解校驗詳解
- 解決Springboot get請求是參數(shù)過長的情況
相關(guān)文章
springboot集成springsecurity 使用OAUTH2做權(quán)限管理的教程
這篇文章主要介紹了springboot集成springsecurity 使用OAUTH2做權(quán)限管理的教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12spring cloud oauth2 實現(xiàn)用戶認(rèn)證登錄的示例代碼
這篇文章主要介紹了spring cloud oauth2 實現(xiàn)用戶認(rèn)證登錄的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10深入dom4j使用selectSingleNode方法報錯分析
本篇文章是對dom4j使用selectSingleNode方法報錯進行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05