黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

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

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

全部教程
主站蜘蛛池模板: www网站在线观看 | 日日草草 | 男女性潮高片无遮挡禁18 | 黄色片地址 | 久久蝌蚪 | 亚洲国产婷婷综合在线精品 | 国内偷自视频区视频综合 | 免费黄色大片视频 | 国产在线精品成人一区二区三区 | tk挠脚心丝袜天堂网站免费控 | 99热网站| 天天干天天操天天操 | 国内外成人免费视频 | 成年人在线免费网站 | 在线制服丝袜 | 天天舔天天插 | 国产在线乱子伦一区二区 | 999国内精品视频免费 | 欧美乱性视频 | 一级片视频免费 | 日韩欧美一区二区三区免费看 | 中文字幕免费在线播放 | 香蕉视频草莓 | 日日噜噜夜夜狠狠扒开双腿 | 日本韩国欧美在线 | 日韩在线专区 | 久久夜夜肉肉热热日日 | 乱人伦视频69 | 国产又爽又黄又舒服又刺激视频 | m男亚洲一区中文字幕 | 欧美久久综合 | 天堂久久久久va久久久久 | 欧美日韩中文在线 | 国产aaa女人十八毛片 | 国产日韩欧美视频在线 | 男女免费观看视频 | 中文字幕久久亚洲一区 | 91精品导航 | 欧美全免费aaaaaa特黄在线 | 欧美亚洲另类一区中文字幕 | 久草欧美 |