SpringBoot實現RESTful

2021-02-20 Java資料站

點擊上方藍色字體,選擇「標星公眾號」

優質文章,第一時間送達

  作者 |  山丘i

來源 |  urlify.cn/iyQrym

一、認識 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 主要是幾個註解實現

2.1 @PathVariable

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

2.2@PostMapping

接收和處理 Post 方式的請求

2.3 @DeleteMapping

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

2.4@PutMapping

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

2.5 @GetMapping

接收 get 方式的請求

三、案例

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

3.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

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

3.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 原則

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

請求路徑不要出現動詞

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

分頁、排序等操作,不需要使用斜槓傳參數
例如:訂單列表接口
/boot/orders?page=1&sort=desc
一般傳的參數不是資料庫表的欄位,可以不採用斜槓

粉絲福利:Java從入門到入土學習路線圖

👇👇👇

感謝點讚支持下哈 

相關焦點

  • Springboot 實現 Restful 服務,基於 HTTP / JSON 傳輸
    REST 是屬於 WEB 自身的一種架構風格,是在 HTTP 1.1 規範下實現的。Representational State Transfer 全稱翻譯為表現層狀態轉化。Resource:資源。比如 newsfeed;Representational:表現形式,比如用JSON,富文本等;State Transfer:狀態變化。通過HTTP 動作實現。
  • 基於SpringBoot開發一個Restful服務,實現增刪改查功能
    在能夠對SpringBoot進行一些簡單的開發Restful風格接口實現CRUD功能之後,於是便有了本篇博文。# SpringBoot介紹Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。
  • SpringBoot教程(1):快速入門CRUD
    4.不需要配置jackson,良好的restful風格支持,自動通過jackson返回json數據。5.個性化配置時,最少一個配置文件可以配置所有的個性化信息。我們這裡不再贅述理論知識,直接上代碼,更多的理論知識,小夥伴們可以自行搜索。需求簡介1.實現Student的增刪該查。2.編碼採用restful風格。
  • 超全的springboot+springsecurity實現前後端分離簡單實現!
    1.2、技術支持  jdk 1.8、springboot 2.3.4、mybatis-plus 3.4.1、mysql 5.5、springsecurity 5.3.4、springmvc、lombok簡化entity代碼,不用你去寫get、set方法,全部自動生成、gson 2.8.2 將json對象轉化成json字符串1.3、預期實現效果圖未登錄時訪問指定資源, 返回未登錄的
  • Nginx+SpringBoot實現負載均衡
    前言本篇文章主要介紹的是Nginx如何實現負載均衡。,實現的一種消息隊列分發機制。upstream xuwujing {random two least_time=last_byte;server www.panchengming.com;server www.panchengming2.com;}Nginx+SpringBoot實現負載均衡環境準備這裡的項目就用本人之前的一個springboot項目,SpringBoot
  • Springboot+Vue實現上傳文件顯示進度條效果功能
    1、springboot+mybatis+vue前後端分離實現用戶登陸註冊功能2、SpringBoot+Vue前後分離實現郵件發送功能3、SpringBoot+Spring Data JPA+Vue前後端分離實現分頁功能4、SpringBoot+Spring Data JPA+Vue前後端分離實現
  • Springboot+websocket實現服務端、客戶端
    一、引言小編最近一直在使用springboot框架開發項目,畢竟現在很多公司都在採用此框架,之後小編也會陸續寫關於springboot開發常用功能的文章。什麼場景下會要使用到websocket的呢?websocket主要功能就是實現網絡通訊,比如說最經典的客服聊天窗口、您有新的消息通知,或者是項目與項目之間的通訊,都可以採用websocket來實現。
  • Nginx+SpringBoot 實現負載均衡
    ,設備商會提供完整成熟的解決方案,比如F5,在數據的穩定性以及安全性來說非常可靠,但是相比軟體而言造價會更加昂貴;軟體的負載均衡以Nginx這類軟體為主,實現的一種消息隊列分發機制。upstream xuwujing { random two least_time=last_byte; server www.panchengming.com; server www.panchengming2.com;}Nginx+SpringBoot實現負載均衡環境準備這個springboot項目地址:https://github.com
  • SpringBoot 實戰 | 集成 Swagger2 構建強大的 RESTful API 文檔
    .paths(PathSelectors.any())                .build();    }    private ApiInfo apiInfo() {        return new ApiInfoBuilder()                                .title("springboot
  • Springboot+Vue實現滑動驗證成功後登錄功能
    1、springboot+mybatis+vue前後端分離實現用戶登陸註冊功能2、SpringBoot+Vue前後分離實現郵件發送功能3、SpringBoot+Spring Data JPA+Vue前後端分離實現分頁功能4、SpringBoot+Spring Data JPA+Vue前後端分離實現
  • SpringBoot+WebSocket實現簡單的數據推送
    websocket響應報文好了,簡單的了解一下,我們來看看springboot+websocket實現推送的過程,以下是個簡單的demo。//發送消息 $('#send').click(function () { websocket.send($('#sMsg').val()); });前端頁面注意:結語今天就簡單的介紹到這裡,有需要這個demo的,可以關注一下小編,後續小編會把代碼上傳到gitee,https://gitee.com/bigqianqian/springboot-websocket
  • Springboot+Vue實現批量文件上傳(pdf、word、excel)並支持在線預覽功能
    1、springboot+mybatis+vue前後端分離實現用戶登陸註冊功能2、SpringBoot+Vue前後分離實現郵件發送功能3、SpringBoot+Spring Data JPA+Vue前後端分離實現分頁功能4、SpringBoot+Spring Data JPA+Vue前後端分離實現
  • SpringBoot實現國際化
    點擊上方藍色字體,選擇「標星公眾號」優質文章,第一時間送達創建好springboot
  • Springboot+layui前後端分離實現word轉pdf功能
    word轉pdf成功後自動下載前端:layui後端:springbo1、springboot+mybatis+vue前後端分離實現用戶登陸註冊功能2、SpringBoot+Vue前後分離實現郵件發送功能3、SpringBoot+Spring Data JPA+Vue前後端分離實現分頁功能4、SpringBoot+Spring Data JPA+Vue前後端分離實現
  • SpringBoot第二十四篇: springboot整合docker
    這篇文篇介紹,怎麼為 springboot程序構建一個docker鏡像。docker 是一個開源的應用容器引擎,基於 Go 語言 並遵從Apache2.0協議開源。Docker 可以讓開發者打包他們的應用以及依賴包到一個輕量級、可移植的容器中,然後發布到任何流行的 Linux 機器上,也可以實現虛擬化。容器是完全使用沙箱機制,相互之間不會有任何接口(類似 iPhone 的 app),更重要的是容器性能開銷極低。
  • Java springMVC] Restful風格API接口設計
    基於這個風格設計的軟體可以更簡潔,更有層次,更易於實現緩存等機制。在Restful風格中,用戶請求的url使用同一個url而用請求方式:get,post,delete,put...等方式對請求的處理方法進行區分,這樣可以在前後臺分離式的開發中使得前端開發人員不會對請求的資源地址產生混淆和大量的檢查方法名的麻煩,形成一個統一的接口。
  • 基於 Spring Boot 實現 Restful 風格接口,實現增刪改查功能
    基於SpringBoot開發一個Restful服務一、開發準備1.1 資料庫和表首先,我們需要在MySql中創建一個資料庫和一張表資料庫的名稱為 springboot,表名稱為 t_user腳本如下:CREATE DATABASE `springboot
  • 基於 Spring Boot 的 Restful 風格實現增刪改查
    在能夠對SpringBoot進行一些簡單的開發Restful風格接口實現CRUD功能之後,於是便有了本篇博文。SpringBoot介紹Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。
  • 基於Nginx+SpringBoot實現10萬+並發,一文講透
    本篇文章主要介紹的是Nginx如何實現負載均衡。,比如F5,在數據的穩定性以及安全性來說非常可靠,但是相比軟體而言造價會更加昂貴;軟體的負載均衡以Nginx這類軟體為主,實現的一種消息隊列分發機制。這裡的項目就用本人之前的一個springboot項目,SpringBoot的項目地址: https://github.com/xuwujing/springBoot-study/tree/master/springboot-thymeleaf
  • 編程 || SpringBoot實現AOP
    AOP概念什麼是aopAOP(切面編程):將相同邏輯的重複代碼橫向抽取出來,使用動態代理技術將這些重複代碼織入到目標對象方法中,實現和原來一樣的功能