大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

SpringBoot教程
SpringBoot入門案例
SpringBoot框架Web開發
SpringBoot非web應用程序
SpringBoot使用攔截器
SpringBoot中使用Servlet
SpringBoot中使用Filter
SpringBoot項目配置字符編碼
SpringBoot打包與部署
SpringBoot使用Actuator
SpringBoot集成Thymeleaf模板
SpringBoot總結及綜合案例
SpringBoot工程下使用Mybatis反向工程

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 主要是幾個注解實現:

@PathVariable:獲取url中的數據,該注解是實現RESTFul最主要的一個注解。

@PostMapping:接收和處理Post方式的請求

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

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

@GetMapping:接收get方式的請求

RESTful的優點

• 輕量,直接基于http,不再需要任何別的諸如消息協議,get/post/put/delete為CRUD操作

• 面向資源,一目了然,具有自解釋性。

• 數據描述簡單,一般以xml,json做數據交換。

• 無狀態,在調用一個接口(訪問、操作資源)的時候,可以不用考慮上下文,不用考慮當前狀態,極大的降低了復雜度。

• 簡單、低耦合

案例:使用RESTful風格模擬實現對學生的增刪改查操作

項目名稱:014-springboot-restful

該項目集成了MyBatis、spring、SpringMVC,通過模擬實現對學生的增刪改查操作。

1.創建RESTfulController,并編寫代碼

@RestController
public class RESTfulController {

    /**
     * 添加學生
     * 請求地址:http://localhost:9090/014-springboot-restful/springBoot/student/wangpeng/23
     * 請求方式:POST
     * @param name
     * @param age
     * @return
     */
    @PostMapping(value = "/springBoot/student/{name}/{age}")
    public Object addStudent(@PathVariable("name") String name,
                             @PathVariable("age") Integer age) {

        Map retMap = new HashMap();
        retMap.put("name",name);
        retMap.put("age",age);


        return retMap;
    }

    /**
     * 刪除學生
     * 請求地址:http://localhost:9090/014-springboot-restful/springBoot/student/1
     * 請求方式:Delete
     * @param id
     * @return
     */
    @DeleteMapping(value = "/springBoot/student/{id}")
    public Object removeStudent(@PathVariable("id") Integer id) {

        return "刪除的學生id為:" + id;
    }

    /**
     * 修改學生信息
     * 請求地址:http://localhost:9090/014-springboot-restful/springBoot/student/2
     * 請求方式:Put
     * @param id
     * @return
     */
    @PutMapping(value = "/springBoot/student/{id}")
    public Object modifyStudent(@PathVariable("id") Integer id) {

        return "修改學生的id為" + id;
    }

    @GetMapping(value = "/springBoot/student/{id}")
    public Object queryStudent(@PathVariable("id") Integer id) {

        return "查詢學生的id為" + id;
    }
}

2.使用Postman模擬發送請求,進行測試

3.總結:其實這里我們能感受到的好處

• 傳遞參數變簡單了

• 服務提供者對外只提供了一個接口服務,而不是傳統的CRUD四個接口

請求沖突的問題

項目名稱:015-springboot-restful-url-conflict

• 改路徑

• 改請求方式

創建RESTfulController類,結合Postman進行測試說明


@RestController
public class RESTfulController {


    /**
     * id:訂單標識
     * status:訂單狀態
     * 請求路徑:http://localhost:9090/015-springboot-restful-url-conflict/springBoot/order/1/1001
     * @param id
     * @param status
     * @return
     */
    @GetMapping(value = "/springBoot/order/{id}/{status}")
    public Object queryOrder(@PathVariable("id") Integer id,
                             @PathVariable("status") Integer status) {

        Map map = new HashMap();

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

        return map;
    }

    /**
     * id:訂單標識
     * status:訂單狀態
     * 請求路徑:http://localhost:9090/015-springboot-restful-url-conflict/springBoot/1/order/1001
     * @param id
     * @param status
     * @return
     */
    @GetMapping(value = "/springBoot/{id}/order/{status}")
    public Object queryOrder1(@PathVariable("id") Integer id,
                              @PathVariable("status") Integer status) {
        Map map = new HashMap();

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

        return map;
    }

    /**
     * id:訂單標識
     * status:訂單狀態
     * 請求路徑:http://localhost:9090/015-springboot-restful-url-conflict/springBoot/1001/order/1
     * @param id
     * @param status
     * @return
     */
    @GetMapping(value = "/springBoot/{status}/order/{id}")
    public Object queryOrder2(@PathVariable("id") Integer id,
                              @PathVariable("status") Integer status) {
        Map map = new HashMap();

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

        return map;
    }

    /**
     * id:訂單標識
     * status:訂單狀態
     * 請求路徑:http://localhost:9090/015-springboot-restful-url-conflict/springBoot/1001/order/1
     * @param id
     * @param status
     * @return
     */
    @PostMapping(value = "/springBoot/{status}/order/{id}")
    public Object queryOrder3(@PathVariable("id") Integer id,
                              @PathVariable("status") Integer status) {
        Map map = new HashMap();

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

        return map;
    }




    /**
     * query1和query2兩個請求路徑會發生請求路徑沖突問題
     * query3與query1和query2發生請求沖突
     * 注意:雖然兩個路徑寫法改變了,但是由于傳遞的兩個參數都是int值,所以不知道該交給哪個請求進行處理
     *      就會出現匹配模糊不清的異常,所以要想解決沖突,有兩種方式:
     *      1.修改請求路徑
     *      2.修改請求方式
     */
}

RESTful原則

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

• 請求路徑不要出現動詞

例如:查詢訂單接口

/boot/order/1021/1(推薦)

/boot/queryOrder/1021/1(不推薦)

• 分頁、排序等操作,不需要使用斜杠傳參數

例如:訂單列表接口 /boot/orders?page=1&sort=desc

一般傳的參數不是數據庫表的字段,可以不采用斜杠

全部教程
主站蜘蛛池模板: 久久国产一片免费观看 | 国产午夜永久福利视频在线观看 | 在线视频一区二区日韩国产 | 国产精品资源网站在线观看 | 欧美aaaa在线观看视频免费 | 亚州综合激情另类久久久 | 久久精品国产亚洲沈樵 | 日韩久久久精品首页 | 欧美日韩毛片 | 青春禁区视频在线观看动漫版 | 久久亚洲国产欧洲精品一 | 日韩 视频在线播放 | 亚欧免费视频 | 天天插天天干天天射 | 国产一级精品高清一级毛片 | 日韩成人精品视频 | 爽爽影院在线免费观看 | 国产专区在线 | 羞羞网站在线看 | 久久中文网中文字幕 | 99久久99热久久精品免 | 四虎4hutv永久在线影院 | 久久999视频 | 天天爽夜夜爽天天做夜夜做 | 亚洲一级色 | 色资源在线观看 | 精品国产一区二区三区香蕉沈先生 | 久久成人毛片 | 色网站在线观看 | 国产亚洲一区二区三区啪 | 免费不卡视频 | 精品视频免费播放 | 黄色影院免费 | 国产成人高清精品免费观看 | 五月婷婷激情六月 | 欧美色视频日本片免费高清 | 日韩二区 | 久久久久久久久久久观看 | 6一10周岁毛片在线 717影院理论午夜伦八戒 | 99re热久久精品这里都是精品 | 欧美va亚洲va在线观看蝴蝶网 |