SpringBoot實現RESTful


一、認識 RESTful

REST (英文:Representational State Transfer ,簡稱 REST )
一種互聯網軟件架構設計的風格,但它並不是標准,它只是提出了一組客戶端和服務器
交互時的架構理念和設計原則,基於這種理念和原則設計的接口可以更簡潔,更有層次,REST
這個詞,是 Roy Thomas Fielding 在他 2000 年的博士論文中提出的。
任何的技術都可以實現這種理念,如果一個架構符合 REST 原則,就稱它為 RESTFul 架構

比如我們要訪問一個 http 接口:http://localhost:8080/boot/order?id=1021&status=1
采用 RESTful 風格則 http 地址為:http://localhost:8080/boot/order/1021/1

二、Spring Boot 開發 RESTful

Spring boot 開發 RESTFul 主要是幾個注解實現

1. @PathVariable

獲取 url 中的數據
現該注解是實現 RESTful 最主要的一個注解

2. @PostMapping

接收和處理 Post 方式的請求

3. @DeleteMapping

接收 delete 方式的請求,可以使用 GetMapping 代替

4. @PutMapping

接收 put 方式的請求,可以用 PostMapping 代替

5. @GetMapping

接收 get 方式的請求

三、案例

創建11-springboot-restful項目,一個基本的springboot項目

1. 實現

1. 建立一個model,里面有Student

package com.md.springboot.model;

/**
 * @author MD
 * @create 2020-08-21 20:20
 */
public class Student {

    private Integer id;
    private String 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;
    }
}

在StudentController中

package com.md.springboot.web;

import com.md.springboot.model.Student;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author MD
 * @create 2020-08-21 20:21
 */

@RestController
public class StudentController {


    @RequestMapping(value = "/student")
    public Object student(Integer id , String name){
        Student student = new Student();
        student.setId(id);
        student.setName(name);

        return student;
    }



    @RequestMapping(value = "student/detail/{id}/{name}")
    public Object student1(@PathVariable("id") Integer id,
                           @PathVariable("name") String name){

        HashMap<Object, Object> retMap = new HashMap<>();

        retMap.put("id",id);
        retMap.put("name",name);

        return retMap;
    }
}

采用普通方式是這樣的:http://localhost:8080/student?id=1001&name=pony

若采用這種風格之后:http://localhost:8080/student/detail/1001/pony

可以根據開發中的需要,是否使用這樣的格式

2. 請求沖突的問題

如果在StudentController有這樣的請求

    @RequestMapping(value = "student/detail/{id}/{name}")
    public Object student1(@PathVariable("id") Integer id,
                           @PathVariable("name") String name){

        HashMap<Object, Object> retMap = new HashMap<>();

        retMap.put("id",id);
        retMap.put("name",name);

        return retMap;
    }


//    和上面的請求路徑沖突
    @RequestMapping(value = "student/detail/{id}/{status}")
    public Object student2(@PathVariable("id") Integer id,
                           @PathVariable("status") String status){

        HashMap<Object, Object> retMap = new HashMap<>();

        retMap.put("id",id);
        retMap.put("status",status);

        return retMap;
    }

此時運行會報錯,會出現路徑沖突

此時有兩種方式

  • 修改請求路徑
  • 修改請求方式

第一個就直接把路徑改了,肯定不會再沖突了,針對第二種方式,可以修改成這樣

 @PostMapping(value = "student/detail/{id}/{name}")
    public Object student1(@PathVariable("id") Integer id,
                           @PathVariable("name") String name){
       Student student = new Student();
       student.setId(id);
       student.setName(name);

       return student;
    }



    @GetMapping(value = "student/detail/{id}/{status}")
    public Object student2(@PathVariable("id") Integer id,
                           @PathVariable("status") String status){

        HashMap<Object, Object> retMap = new HashMap<>();

        retMap.put("id",id);
        retMap.put("status",status);

        return retMap;
    }

一個使用post請求,一個使用get請求,這樣就不會報錯了

![](https://img2020.cnblogs.com/blog/1212924/202012/1212924-20201225222518548-1752399728.png

四、RESTful 原則

  1. 增 post 請求、刪 delete 請求、改 put 請求、查 get 請求

  2. 請求路徑不要出現動詞

    例如:查詢訂單接口
    /boot/order/1021/1(推薦)
    /boot/queryOrder/1021/1(不推薦)

  3. 分頁、排序等操作,不需要使用斜杠傳參數
    例如:訂單列表接口
    /boot/orders?page=1&sort=desc
    一般傳的參數不是數據庫表的字段,可以不采用斜杠


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM