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 請(qǐng)求的參數(shù)寫在后臺(tái)方法的形參中,允許參數(shù)為空。
- HTTP 請(qǐng)求的參數(shù)值(字符串)將自動(dòng)轉(zhuǎn)換為方法形參的類型。
- 要求:方法的參數(shù)名稱要和 HTTP 請(qǐng)求的參數(shù)名稱保持一致。
- 適用于 GET 和 POST 請(qǐng)求方式。
后臺(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嘗試:
前端請(qǐng)求:http://localhost:8080/test1?id=2019001&name=小明
參數(shù)是 key=value 形式
value 默認(rèn)都是字符串類型
參數(shù)和請(qǐng)求之間用?連接
參數(shù)之間用&連接

為空值的情況:

使用HttpServletRequest對(duì)象
方法的參數(shù)中添加 HttpServletRequest 對(duì)象。
通過該對(duì)象 getParameter(“xxx”) 獲取請(qǐng)求的 “xxx” 參數(shù)值(字符串值)。
適用于 GET 和 POST 請(qǐng)求方式。
后臺(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測(cè)試:

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

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

為空:

名稱不一致:

使用 @RequestParam 獲取參數(shù)
在無注解的情況下,要求 HTTP 參數(shù)名與控制器參數(shù)名稱保持一致,然
而現(xiàn)在在前后臺(tái)分離的趨勢(shì)下,前端的命名規(guī)則可能與后端的不一樣。
使用 @RequestParam 注解來確定前后端參數(shù)名稱的映射關(guān)系。
適用于 GET 和 POST 請(qǐng)求方式。
@RequestParam 有三個(gè)配置參數(shù):
value 為接收 HTTP 請(qǐng)求的參數(shù)名。
required 表示是否必須,默認(rèn)為 true,表示參數(shù)必填。
defaultValue 可設(shè)置請(qǐng)求參數(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測(cè)試:


報(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測(cè)試:

全為空:

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

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

@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測(cè)試:

前端傳遞對(duì)象數(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測(cè)試:

控制器代碼完整版:
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot自定義動(dòng)態(tài)數(shù)據(jù)源的流程步驟
動(dòng)態(tài)數(shù)據(jù)源,本質(zhì)上是把多個(gè)數(shù)據(jù)源存儲(chǔ)在一個(gè)?Map?中,當(dāng)需要使用某一個(gè)數(shù)據(jù)源時(shí),使用?key?獲取指定數(shù)據(jù)源進(jìn)行處理,本文將給大家介紹一下SpringBoot自定義動(dòng)態(tài)數(shù)據(jù)源的流程步驟,需要的朋友可以參考下2024-06-06
springboot掃描引入jar包的service等組件方式
這篇文章主要介紹了springboot掃描引入jar包的service等組件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java編程實(shí)現(xiàn)游戲中的簡(jiǎn)單碰撞檢測(cè)功能示例
這篇文章主要介紹了Java編程中的簡(jiǎn)單碰撞檢測(cè)功能,涉及java針對(duì)坐標(biāo)點(diǎn)的相關(guān)數(shù)學(xué)運(yùn)算操作技巧,需要的朋友可以參考下2017-10-10
Java中IO流 RandomAccessFile類實(shí)例詳解
這篇文章主要介紹了Java中IO流 RandomAccessFile類實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Java獲取時(shí)間打印到控制臺(tái)代碼實(shí)例
這篇文章主要介紹了Java獲取時(shí)間打印到控制臺(tái)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
在logback.xml中自定義動(dòng)態(tài)屬性的方法
這篇文章主要介紹了在logback.xml中自定義動(dòng)態(tài)屬性的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08

