一:獲取參數(shù)

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

截圖

示例代碼:

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請(qǐng)求的參數(shù)可以通過(guò)@PathVariable和@RequestParam獲取
     * @param id 必填
     * @param name 必填
     * @param price 選填,默認(rèn)值為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格式的參數(shù)自動(dòng)綁定到Entity類(lèi)
     * @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獲取請(qǐng)求體中非Json格式的數(shù)據(jù)
     * @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請(qǐng)求也可以直接與對(duì)象類(lèi)綁定,但需要參數(shù)名一致,不支持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請(qǐng)求可以直接與對(duì)象類(lèi)綁定,但需要參數(shù)名一致
     * @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;
    }


}

注意點(diǎn):

1.針對(duì)一些非必填的參數(shù),可以使用required關(guān)鍵字來(lái)標(biāo)識(shí),同時(shí)必須設(shè)置默認(rèn)值defaultValue,如getOrder方法中對(duì)price參數(shù)的獲取:

@RequestParam(value = "price",required = false,defaultValue = "0") Integer price

2.參數(shù)可以直接與Entity類(lèi)綁定,但不支持json格式,只支持form-data和x-www.form-urlencoded格式

@PostMapping("/order/add")

public String addOrder(Order order){

3.使用的Postman做的測(cè)試,所有接口都測(cè)試通過(guò),也推薦大家使用Postman作為日常的接口測(cè)試工具,安裝和操作都很簡(jiǎn)單。

 附部分截圖:

 Get:@PathVariable,@RequestParam

截圖

Post:@RequestBody

截圖

獲取到參數(shù)以后就是要對(duì)數(shù)據(jù)做校驗(yàn)了,在下一篇中進(jìn)行介紹