Spring Boot 控制層之參數(shù)傳遞方法詳解
當然,您自己創(chuàng)建一個項目也是可以的。
bean包下的Student.java
package com.example.demo.bean;
public class Student {
private Integer id; //學號
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ù)寫在后臺方法的形參中,允許參數(shù)為空。
- HTTP 請求的參數(shù)值(字符串)將自動轉換為方法形參的類型。
- 要求:方法的參數(shù)名稱要和 HTTP 請求的參數(shù)名稱保持一致。
- 適用于 GET 和 POST 請求方式。
后臺代碼:
@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 默認都是字符串類型
參數(shù)和請求之間用?連接
參數(shù)之間用&連接

為空值的情況:

使用HttpServletRequest對象
方法的參數(shù)中添加 HttpServletRequest 對象。
通過該對象 getParameter(“xxx”) 獲取請求的 “xxx” 參數(shù)值(字符串值)。
適用于 GET 和 POST 請求方式。
后臺代碼:
@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ù),Spring Boot會自動創(chuàng)建這個類的對象,并用 HTTP 參數(shù)裝配它。
要求:實體類屬性名稱要和 HTTP 請求的參數(shù)名稱保持一致。
適用于 GET 和 POST 請求方式。
后臺代碼:
@RequestMapping("/test3")
public Student test3( Student s ) {
return s;
}
要求:實體類屬性名稱要和 HTTP 請求的參數(shù)名稱保持一致
postman測試:
一般情況:

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

為空:

名稱不一致:

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


報錯:stu_name參數(shù)默認必填

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

錯誤的前端請求:

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

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

前端傳遞對象數(shù)組
后臺代碼
@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") ); // 轉換一下(沒有判斷空串或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;
}
}
到此這篇關于Spring Boot 控制層之參數(shù)傳遞方法詳解的文章就介紹到這了,更多相關Spring Boot 參數(shù)傳遞內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot自定義動態(tài)數(shù)據(jù)源的流程步驟
動態(tài)數(shù)據(jù)源,本質(zhì)上是把多個數(shù)據(jù)源存儲在一個?Map?中,當需要使用某一個數(shù)據(jù)源時,使用?key?獲取指定數(shù)據(jù)源進行處理,本文將給大家介紹一下SpringBoot自定義動態(tài)數(shù)據(jù)源的流程步驟,需要的朋友可以參考下2024-06-06
springboot掃描引入jar包的service等組件方式
這篇文章主要介紹了springboot掃描引入jar包的service等組件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Java中IO流 RandomAccessFile類實例詳解
這篇文章主要介紹了Java中IO流 RandomAccessFile類實例詳解的相關資料,需要的朋友可以參考下2017-05-05

