Spring Boot 控制層之參數(shù)傳遞方法詳解
當(dāng)然,您自己創(chuàng)建一個(gè)項(xiàng)目也是可以的。
bean包下的Student.java
package com.example.demo.bean;
public class Student {
private Integer id; //學(xué)號(hào)
private String name; //姓名
public Student() {
}
public Student(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
無注解獲取參數(shù)
- 直接把 HTTP 請求的參數(shù)寫在后臺(tái)方法的形參中,允許參數(shù)為空。
- HTTP 請求的參數(shù)值(字符串)將自動(dòng)轉(zhuǎn)換為方法形參的類型。
- 要求:方法的參數(shù)名稱要和 HTTP 請求的參數(shù)名稱保持一致。
- 適用于 GET 和 POST 請求方式。
后臺(tái)代碼:
@RestController
public class TestController {
@RequestMapping("/test1")
public Student test1(Integer id, String name) {
Student s=new Student();
s.setId(id);
s.setName(name);
return s;
}
}
前端代碼:
<body>
<p th:inline = "none" >hello, [[${msg}]]</p>
<p th:inline = "none" >hello, [(${msg})]</p>
</body>
用postman嘗試:
前端請求:http://localhost:8080/test1?id=2019001&name=小明
參數(shù)是 key=value 形式
value 默認(rèn)都是字符串類型
參數(shù)和請求之間用?連接
參數(shù)之間用&連接

為空值的情況:

使用HttpServletRequest對象
方法的參數(shù)中添加 HttpServletRequest 對象。
通過該對象 getParameter(“xxx”) 獲取請求的 “xxx” 參數(shù)值(字符串值)。
適用于 GET 和 POST 請求方式。
后臺(tái)代碼:
@RequestMapping("/test2")
public Student test2( HttpServletRequest request ) {
Integer id = Integer.parseInt( request.getParameter("id") );
String name = request.getParameter("name");
Student s=new Student(id,name);
return s;
}
postman測試:

使用實(shí)體類封裝 ★
直接使用實(shí)體類做為控制器參數(shù),Spring Boot會(huì)自動(dòng)創(chuàng)建這個(gè)類的對象,并用 HTTP 參數(shù)裝配它。
要求:實(shí)體類屬性名稱要和 HTTP 請求的參數(shù)名稱保持一致。
適用于 GET 和 POST 請求方式。
后臺(tái)代碼:
@RequestMapping("/test3")
public Student test3( Student s ) {
return s;
}
要求:實(shí)體類屬性名稱要和 HTTP 請求的參數(shù)名稱保持一致
postman測試:
一般情況:

多的參數(shù)不接收:

為空:

名稱不一致:

使用 @RequestParam 獲取參數(shù)
在無注解的情況下,要求 HTTP 參數(shù)名與控制器參數(shù)名稱保持一致,然
而現(xiàn)在在前后臺(tái)分離的趨勢下,前端的命名規(guī)則可能與后端的不一樣。
使用 @RequestParam 注解來確定前后端參數(shù)名稱的映射關(guān)系。
適用于 GET 和 POST 請求方式。
@RequestParam 有三個(gè)配置參數(shù):
value 為接收 HTTP 請求的參數(shù)名。
required 表示是否必須,默認(rèn)為 true,表示參數(shù)必填。
defaultValue 可設(shè)置請求參數(shù)的默認(rèn)值。
后臺(tái)代碼:
@RequestMapping("/test4")
public Student test4( @RequestParam(value="stu_id") Integer id,
@RequestParam(value="stu_name") String name) {
Student s=new Student();
s.setId(id);
s.setName(name);
return s;
}
postman測試:


報(bào)錯(cuò):stu_name參數(shù)默認(rèn)必填

添加默認(rèn)值
@RequestMapping("/test4")
public Student test4(@RequestParam(value="stu_id", required = true, defaultValue = "2019000") Integer id,
@RequestParam(value="stu_name", required = false) String name) {
Student s=new Student();
s.setId(id);
s.setName(name);
return s;
}
postman測試:

全為空:

使用 @PathVariable 獲取參數(shù)
REST風(fēng)格請求,例如:http://localhost:8080/test4/2019001/小明(數(shù)據(jù)直接放在請求路徑上,并用"/“連接)
后臺(tái)請求映射:
@RequestMapping(”/test4/{id}/{name}")
參數(shù)綁定:
@PathVariable(“xxx”) 將占位符參數(shù)"xxx"綁定到控制器方法的形參中。
注意:
(1)路徑上必須有參數(shù)(required=true),且不能設(shè)置默認(rèn)值。
(2)適用于 GET 和 POST 請求方式。
后臺(tái)代碼:
@RequestMapping("/test5/{id}/{name}")
public Student test5( @PathVariable("id") Integer id,//這里有兩種寫法
@PathVariable(value = "name") String name) {
Student s=new Student();
s.setId(id);
s.setName(name);
return s;
}
前端請求:(這次不用postman測試了,用也可以,都一樣)

錯(cuò)誤的前端請求:

@PathVariable與@RequestParam的區(qū)別

使用 @RequestBody 獲取參數(shù)
@RequestBody 主要用來接收前端傳遞的 json 數(shù)據(jù)。
注意:
(1)使用 @RequestBody 接收數(shù)據(jù)時(shí),前端必須 POST 方式提交;
(2)且必須與 contentType = “application/json” 配合使用。
不設(shè)置時(shí)使用的是默認(rèn)值:application/x-www-form-urlencoded
后臺(tái)代碼
@PostMapping("/test6")
public Student test6(@RequestBody Student s) {
return s;
}
postman測試:

前端傳遞對象數(shù)組
后臺(tái)代碼
@PostMapping("/test6")
public List<Student> test6(@RequestBody List<Student> list) {
Iterator it = list.iterator();
while (it.hasNext()){
Student s=(Student)it.next();
System.out.println(s.getId()+":"+s.getName());
}
return list;
}
postman測試:

控制器代碼完整版:
TestController.java
package com.example.demo.controller;
import com.example.demo.bean.Student;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
@RestController
@ServletComponentScan
public class TestController {
@RequestMapping("/test1")
public Student test1(Integer id, String name) {
Student s=new Student();
s.setId(id);
s.setName(name);
return s;
}
@RequestMapping("/test2")
public Student test2( HttpServletRequest request ) {
Integer id = Integer.parseInt( request.getParameter("id") ); // 轉(zhuǎn)換一下(沒有判斷空串或null)
String name = request.getParameter("name");
Student s=new Student(id,name);
return s;
}
@RequestMapping("/test3")
public Student test3( Student s ) {
return s;
}
@RequestMapping("/test4")
public Student test4(@RequestParam(value="stu_id", required = true, defaultValue = "2019000") Integer id,
@RequestParam(value="stu_name", required = false) String name) {
Student s=new Student();
s.setId(id);
s.setName(name);
return s;
}
@RequestMapping("/test5/{id}/{name}")
public Student test5( @PathVariable("id") Integer id,//這里有兩種寫法
@PathVariable(value = "name") String name) {
Student s=new Student();
s.setId(id);
s.setName(name);
return s;
}
@PostMapping("/test6")
public List<Student> test6(@RequestBody List<Student> list) {
Iterator it = list.iterator();
while (it.hasNext()){
Student s=(Student)it.next();
System.out.println(s.getId()+":"+s.getName());
}
return list;
}
}
到此這篇關(guān)于Spring Boot 控制層之參數(shù)傳遞方法詳解的文章就介紹到這了,更多相關(guān)Spring Boot 參數(shù)傳遞內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring+hibernate 兩種整合方式配置文件的方法
本篇文章主要介紹了spring+hibernate 兩種整合方式配置文件的方法,主要有兩種方式 1、注解方式 2、xml方式實(shí)現(xiàn),有興趣的可以了解一下。2017-04-04
springboot @RequestBody 接收字符串實(shí)例
這篇文章主要介紹了springboot @RequestBody 接收字符串實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Java中的Random()函數(shù)及兩種構(gòu)造方法
Java中存在著兩種Random函數(shù)分別是java.lang.Math.Random和java.util.Random,文中給大家介紹了random()的兩種構(gòu)造方法,感興趣的朋友跟隨小編一起看看吧2018-11-11
RabbitMQ實(shí)現(xiàn)Work Queue工作隊(duì)列的示例詳解
工作隊(duì)列(又稱任務(wù)隊(duì)列)的主要思想是避免立即執(zhí)行資源密集型任務(wù),而不得不等待它完成。本篇文章將記錄和分享RabbitMQ工作隊(duì)列相關(guān)的知識(shí)點(diǎn),希望對大家有所幫助2023-01-01
java 使用JDBC構(gòu)建簡單的數(shù)據(jù)訪問層實(shí)例詳解
以下是如何使用JDBC構(gòu)建一個(gè)數(shù)據(jù)訪問層,包括數(shù)據(jù)轉(zhuǎn)換(將從數(shù)據(jù)庫中查詢的數(shù)據(jù)封裝到對應(yīng)的對象中……),數(shù)據(jù)庫的建立,以及如何連接到數(shù)據(jù)庫,需要的朋友可以參考下2016-11-11
Spring自定義注解實(shí)現(xiàn)接口版本管理
這篇文章主要介紹了Spring自定義注解實(shí)現(xiàn)接口版本管理,RequestMappingHandlerMapping類是與 @RequestMapping相關(guān)的,它定義映射的規(guī)則,即滿足怎樣的條件則映射到那個(gè)接口上,需要的朋友可以參考下2023-11-11

