上一篇SpringBoot實戰(二)Restful風格API接口中寫了一個控制器,獲取了前端請求的參數,現在我們就參數的獲取與校驗做一個介紹:
一:獲取參數
SpringBoot提供的獲取參數注解包括:@PathVariable,@RequestParam,@RequestBody,三者的區別如下表:

示例代碼:
Order:
1 package com.example.demo.controller.user.entity; 2 3 public class Order { 4 private Integer id; 5 private String name; 6 private Integer price; 7 8 public Integer getId() { 9 return id; 10 } 11 12 public void setId(Integer id) { 13 this.id = id; 14 } 15 16 public String getName() { 17 return name; 18 } 19 20 public void setName(String name) { 21 this.name = name; 22 } 23 24 public Integer getPrice() { 25 return price; 26 } 27 28 public void setPrice(Integer price) { 29 this.price = price; 30 } 31 }
OrderController
1 package com.example.demo.controller.user.controller; 2 3 import com.example.demo.controller.user.entity.Order; 4 import org.springframework.web.bind.annotation.*; 5 6 @RestController 7 public class OrderController { 8 9 /** 10 * Get請求的參數可以通過@PathVariable和@RequestParam獲取 11 * @param id 必填 12 * @param name 必填 13 * @param price 選填,默認值為0 14 * @return 15 */ 16 @GetMapping("/orders/{id}") 17 public String getOrder(@PathVariable(value = "id")Integer id, 18 @RequestParam(value = "name")String name, 19 @RequestParam(value = "price",required = false,defaultValue = "0") Integer price){ 20 String result = "id:"+id+",name:"+name+",price:"+price; 21 return result; 22 } 23 24 /** 25 * Post使用@RequestBody注解將Json格式的參數自動綁定到Entity類 26 * @param order 27 * @return 28 */ 29 @PostMapping("/order/check") 30 public String checkOrder(@RequestBody Order order){ 31 String result = "id:"+order.getId()+",name:"+order.getName()+",price:"+order.getPrice(); 32 return result; 33 } 34 35 /** 36 * Post使用@RequestParam獲取請求體中非Json格式的數據 37 * @param amount 38 * @param discount 39 * @return 40 */ 41 @PostMapping("/order/checkmore") 42 public String checkMore(@RequestParam(value = "amount")Integer amount, @RequestParam(value = "discount")float discount){ 43 String result = "amount:"+amount+",discount:"+discount; 44 return result; 45 } 46 47 /** 48 * Post請求也可以直接與對象類綁定,但需要參數名一致,不支持json格式,只支持form-data和x-www.form-urlencoded格式 49 * @param order 50 * @return 51 */ 52 @PostMapping("/order/add") 53 public String addOrder(Order order){ 54 String result = "id:"+order.getId()+",name:"+order.getName()+",price:"+order.getPrice(); 55 return result; 56 } 57 58 /** 59 * Put請求可以直接與對象類綁定,但需要參數名一致 60 * @param id 61 * @param order 62 * @return 63 */ 64 @PutMapping("/order/{id}/update") 65 public String updateOrder(@PathVariable(value = "id")Integer id,Order order){ 66 String result = "pathid:"+id+"===Order(id:"+order.getId()+",name:"+order.getName()+",price:"+order.getPrice()+")"; 67 return result; 68 } 69 70 71 }
注意點:
1.針對一些非必填的參數,可以使用required關鍵字來標識,同時必須設置默認值defaultValue,如getOrder方法中對price參數的獲取:
@RequestParam(value = "price",required = false,defaultValue = "0") Integer price
2.參數可以直接與Entity類綁定,但不支持json格式,只支持form-data和x-www.form-urlencoded格式
@PostMapping("/order/add")
public String addOrder(Order order){
3.使用的Postman做的測試,所有接口都測試通過,也推薦大家使用Postman作為日常的接口測試工具,安裝和操作都很簡單。
附部分截圖:
Get:@PathVariable,@RequestParam

Post:@RequestBody

獲取到參數以后就是要對數據做校驗了,在下一篇中進行介紹
