一:獲取參數

SpringBoot提供的獲取參數注解包括:@PathVariable,@RequestParam,@RequestBody,三者的區別如下表:

截圖

示例代碼:

Order:

package com.example.demo.controller.user.entity;

public class Order {
    private Integer id;
    private String name;
    private Integer price;

    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;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }
}
OrderController

package com.example.demo.controller.user.controller;

import com.example.demo.controller.user.entity.Order;
import org.springframework.web.bind.annotation.*;

@RestController
public class OrderController {

    /**
     * Get請求的參數可以通過@PathVariable和@RequestParam獲取
     * @param id 必填
     * @param name 必填
     * @param price 選填,默認值為0
     * @return
     */
    @GetMapping("/orders/{id}")
    public String getOrder(@PathVariable(value = "id")Integer id,
                           @RequestParam(value = "name")String name,
                           @RequestParam(value = "price",required = false,defaultValue = "0") Integer price){
        String result = "id:"+id+",name:"+name+",price:"+price;
        return result;
    }

    /**
     * Post使用@RequestBody注解將Json格式的參數自動綁定到Entity類
     * @param order
     * @return
     */
    @PostMapping("/order/check")
    public String checkOrder(@RequestBody Order order){
        String result = "id:"+order.getId()+",name:"+order.getName()+",price:"+order.getPrice();
        return result;
    }

    /**
     * Post使用@RequestParam獲取請求體中非Json格式的數據
     * @param amount
     * @param discount
     * @return
     */
    @PostMapping("/order/checkmore")
    public String checkMore(@RequestParam(value = "amount")Integer amount, @RequestParam(value = "discount")float discount){
        String result = "amount:"+amount+",discount:"+discount;
        return result;
    }

    /**
     * Post請求也可以直接與對象類綁定,但需要參數名一致,不支持json格式,只支持form-data和x-www.form-urlencoded格式
     * @param order
     * @return
     */
    @PostMapping("/order/add")
    public String addOrder(Order order){
        String result = "id:"+order.getId()+",name:"+order.getName()+",price:"+order.getPrice();
        return result;
    }

    /**
     * Put請求可以直接與對象類綁定,但需要參數名一致
     * @param id
     * @param order
     * @return
     */
    @PutMapping("/order/{id}/update")
    public String updateOrder(@PathVariable(value = "id")Integer id,Order order){
        String result = "pathid:"+id+"===Order(id:"+order.getId()+",name:"+order.getName()+",price:"+order.getPrice()+")";
        return result;
    }


}

注意點:

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

截圖

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