[JAVA EE]Spring Boot 控制层:参数传递方法
項(xiàng)目需要參照本人java ee系列前文建立,請讀者自行前往本人主頁查看。
當(dāng)然,您自己創(chuàng)建一個(gè)項(xiàng)目也是可以的。
- bean包下的Student.java
package com.example.demo.bean;
public class Student {private Integer id; //學(xué)號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ù)值(字符串)將自動(dòng)轉(zhuǎn)換為方法形參的類型。
? 要求:方法的參數(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 默認(rèn)都是字符串類型
參數(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í)體類封裝 ★
? 直接使用實(shí)體類做為控制器參數(shù),Spring Boot會(huì)自動(dòng)創(chuàng)建這個(gè)類的對象,并用 HTTP 參數(shù)裝配它。
? 要求:實(shí)體類屬性名稱要和 HTTP 請求的參數(shù)名稱保持一致。
? 適用于 GET 和 POST 請求方式。
后臺代碼:
@RequestMapping("/test3")public Student test3( Student s ) {return s;}
要求:實(shí)體類屬性名稱要和 HTTP 請求的參數(shù)名稱保持一致
postman測試:
一般情況:
多的參數(shù)不接收:
為空:
名稱不一致:
使用 @RequestParam 獲取參數(shù)
? 在無注解的情況下,要求 HTTP 參數(shù)名與控制器參數(shù)名稱保持一致,然
而現(xiàn)在在前后臺分離的趨勢下,前端的命名規(guī)則可能與后端的不一樣。
? 使用 @RequestParam 注解來確定前后端參數(shù)名稱的映射關(guān)系。
? 適用于 GET 和 POST 請求方式。
? @RequestParam 有三個(gè)配置參數(shù):
? value 為接收 HTTP 請求的參數(shù)名。
? required 表示是否必須,默認(rèn)為 true,表示參數(shù)必填。
? defaultValue 可設(shè)置請求參數(shù)的默認(rèn)值。
后臺代碼:
@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ù)直接放在請求路徑上,并用"/“連接)
? 后臺請求映射:
@RequestMapping(”/test4/{id}/{name}")
? 參數(shù)綁定:
@PathVariable(“xxx”) 將占位符參數(shù)"xxx"綁定到控制器方法的形參中。
? 注意:
(1)路徑上必須有參數(shù)(required=true),且不能設(shè)置默認(rèn)值。
(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測試了,用也可以,都一樣)
錯(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
后臺代碼
@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") ); // 轉(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;}}
總結(jié)
以上是生活随笔為你收集整理的[JAVA EE]Spring Boot 控制层:参数传递方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个霸气侧漏的个性签名。
- 下一篇: [JAVA EE] Filter过滤器