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

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

SpringBoot實(shí)現(xiàn)RESTful

認(rèn)識(shí) RESTFul

REST(英文:Representational State Transfer,簡稱REST)

一種互聯(lián)網(wǎng)軟件架構(gòu)設(shè)計(jì)的風(fēng)格,但它并不是標(biāo)準(zhǔn),它只是提出了一組客戶端和服務(wù)器交互時(shí)的架構(gòu)理念和設(shè)計(jì)原則,基于這種理念和原則設(shè)計(jì)的接口可以更簡潔,更有層次,REST這個(gè)詞,是Roy Thomas Fielding在他2000年的博士論文中提出的。

任何的技術(shù)都可以實(shí)現(xiàn)這種理念,如果一個(gè)架構(gòu)符合REST原則,就稱它為RESTFul架構(gòu)。

比如我們要訪問一個(gè)http接口:http://localhost:8080/boot/order?id=1021&status=1

采用RESTFul風(fēng)格則http地址為:http://localhost:8080/boot/order/1021/1

Spring Boot開發(fā)RESTFul

Spring boot開發(fā)RESTFul 主要是幾個(gè)注解實(shí)現(xiàn):

@PathVariable:獲取url中的數(shù)據(jù),該注解是實(shí)現(xiàn)RESTFul最主要的一個(gè)注解。

@PostMapping:接收和處理Post方式的請(qǐng)求

@DeleteMapping:接收delete方式的請(qǐng)求,可以使用GetMapping代替

@PutMapping:接收put方式的請(qǐng)求,可以用PostMapping代替

@GetMapping:接收get方式的請(qǐng)求

RESTful的優(yōu)點(diǎn)

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

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

• 數(shù)據(jù)描述簡單,一般以xml,json做數(shù)據(jù)交換。

• 無狀態(tài),在調(diào)用一個(gè)接口(訪問、操作資源)的時(shí)候,可以不用考慮上下文,不用考慮當(dāng)前狀態(tài),極大的降低了復(fù)雜度。

• 簡單、低耦合

案例:使用RESTful風(fēng)格模擬實(shí)現(xiàn)對(duì)學(xué)生的增刪改查操作

項(xiàng)目名稱:014-springboot-restful

該項(xiàng)目集成了MyBatis、spring、SpringMVC,通過模擬實(shí)現(xiàn)對(duì)學(xué)生的增刪改查操作。

1.創(chuàng)建RESTfulController,并編寫代碼

@RestController
public class RESTfulController {

    /**
     * 添加學(xué)生
     * 請(qǐng)求地址:http://localhost:9090/014-springboot-restful/springBoot/student/wangpeng/23
     * 請(qǐng)求方式: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;
    }

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

        return "刪除的學(xué)生id為:" + id;
    }

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

        return "修改學(xué)生的id為" + id;
    }

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

        return "查詢學(xué)生的id為" + id;
    }
}

2.使用Postman模擬發(fā)送請(qǐng)求,進(jìn)行測試

3.總結(jié):其實(shí)這里我們能感受到的好處

• 傳遞參數(shù)變簡單了

• 服務(wù)提供者對(duì)外只提供了一個(gè)接口服務(wù),而不是傳統(tǒng)的CRUD四個(gè)接口

請(qǐng)求沖突的問題

項(xiàng)目名稱:015-springboot-restful-url-conflict

• 改路徑

• 改請(qǐng)求方式

創(chuàng)建RESTfulController類,結(jié)合Postman進(jìn)行測試說明


@RestController
public class RESTfulController {


    /**
     * id:訂單標(biāo)識(shí)
     * status:訂單狀態(tài)
     * 請(qǐng)求路徑: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:訂單標(biāo)識(shí)
     * status:訂單狀態(tài)
     * 請(qǐng)求路徑: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:訂單標(biāo)識(shí)
     * status:訂單狀態(tài)
     * 請(qǐng)求路徑: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:訂單標(biāo)識(shí)
     * status:訂單狀態(tài)
     * 請(qǐng)求路徑: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兩個(gè)請(qǐng)求路徑會(huì)發(fā)生請(qǐng)求路徑?jīng)_突問題
     * query3與query1和query2發(fā)生請(qǐng)求沖突
     * 注意:雖然兩個(gè)路徑寫法改變了,但是由于傳遞的兩個(gè)參數(shù)都是int值,所以不知道該交給哪個(gè)請(qǐng)求進(jìn)行處理
     *      就會(huì)出現(xiàn)匹配模糊不清的異常,所以要想解決沖突,有兩種方式:
     *      1.修改請(qǐng)求路徑
     *      2.修改請(qǐng)求方式
     */
}

RESTful原則

• 增post請(qǐng)求、刪delete請(qǐng)求、改put請(qǐng)求、查get請(qǐng)求

• 請(qǐng)求路徑不要出現(xiàn)動(dòng)詞

例如:查詢訂單接口

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

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

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

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

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

全部教程
主站蜘蛛池模板: 亚洲欧美日韩精品一区 | 日本高清中文字幕一区二区三区a | 国产综合精品在线 | 亚洲成人在线免费 | 国产精品人成在线播放新网站 | 成人999| 全黄大全大色全免费大片 | 图片亚洲va欧美va国产综合 | 成人精品一区二区三区校园激情 | 在线播放成人毛片免费视 | 国语性猛交xxxx乱大交 | 亚洲综合国产一区二区三区 | 99久久99久久精品免费看子 | 涩涩视频免费观看 | 久久思re热9一区二区三区 | 亚色在线播放 | 亚洲国产99999在线精品一区 | 99re热视频精品首页 | 亚洲国产日韩欧美综合久久 | 中国一级特黄aa毛片大片 | 色拍拍噜噜噜aⅴ在线观看 色拍拍欧美视频在线看 | 久久浮力影院 | 97久久精品国产精品青草 | 四虎国产精品永久地址48 | 国产人成精品 | 国产成人综合一区精品 | 97在线看 | 四虎影院视频 | 牛牛影视午夜免费福利 | 奇米影视第四色首页 | 有色视频在线观看 | 亚洲国产精品区 | 成人免费淫片免费观看 | 精品国产90后在线观看 | 四虎成年永久免费网站 | 综合婷婷丁香 | 一级毛片免费不卡在线 | 四虎影视免费永久在线观看 | 九九这里只有精品 | 中文国产成人精品久久96 | 天天色综合5|