Restful學(xué)習(xí)

1.Rest簡(jiǎn)介

  1. REST是英文representational state transfer(表象性狀態(tài)轉(zhuǎn)變)或者表述性狀態(tài)轉(zhuǎn)移。
  2. Rest是web服務(wù)的一種架構(gòu)風(fēng)格。
  3. 使用HTTP,URI,XML,JSON,HTML等廣泛流行的標(biāo)準(zhǔn)和協(xié)議。
  4. 輕量級(jí),跨平臺(tái),跨語(yǔ)言的架構(gòu)設(shè)計(jì)。
  5. 它是一種設(shè)計(jì)風(fēng)格,不是一種標(biāo)準(zhǔn),是一種思想。

2.Rest架構(gòu)的主要原則

  1. 網(wǎng)絡(luò)上的所有事物都被抽象為資源
  2. 每個(gè)資源都有一個(gè)唯一的資源標(biāo)識(shí)符
  3. 同一個(gè)資源具有多種表現(xiàn)形式(xml,json等)
  4. 對(duì)資源的各種操作不會(huì)改變資源標(biāo)識(shí)符
  5. 所有的操作都是無(wú)狀態(tài)的
  6. 符合REST原則的架構(gòu)方式即可稱為RESTful

3.什么是Restful

  1. 對(duì)應(yīng)的中文是rest式的
  2. Restful web service是一種常見的rest的應(yīng)用,是遵守了rest風(fēng)格的web服務(wù)
  3. rest式的web服務(wù)是一種ROA(The Resource-Oriented Architecture)(面向資源的架構(gòu))

4.為什么會(huì)出現(xiàn)Restful

在Restful之前的操作

  1. http://127.0.0.1/user/query GET 根據(jù)用戶id查詢用戶數(shù)據(jù)
  2. http://127.0.0.1/user/save POST 新增用戶
  3. http://127.0.0.1/user/update POST 修改用戶信息
  4. http://127.0.0.1/user/delete GET/POST 刪除用戶信息

Restful用法

  1. http://127.0.0.1/user/1 GET 根據(jù)用戶id查詢用戶數(shù)據(jù)
  2. http://127.0.0.1/user POST 新增用戶
  3. http://127.0.0.1/user PUT 修改用戶信息
  4. http://127.0.0.1/user DELETE 刪除用戶信息

常用的HTTP動(dòng)詞有下面五個(gè)(括號(hào)里是對(duì)應(yīng)的SQL命令)

  1. GET(SELECT):從服務(wù)器取出資源(一項(xiàng)或多項(xiàng))。
  2. POST(CREATE):在服務(wù)器新建一個(gè)資源。
  3. PUT(UPDATE):在服務(wù)器更新資源(客戶端提供改變后的完整資源)。
  4. PATCH(UPDATE):在服務(wù)器更新資源(客戶端提供改變的屬性)。
  5. DELETE(DELETE):從服務(wù)器刪除資源。

5.如何使用

http方法 資源操作 冪等 安全
GET SELECT
POST INSERT
PUT UPDATE
DELETE DELETE

冪等性:對(duì)同一REST接口的多次訪問(wèn),得到的資源狀態(tài)是相同的。
安全性:對(duì)該REST接口訪問(wèn),不會(huì)使服務(wù)器端資源的狀態(tài)發(fā)生改變。

6.SpringMVC原生態(tài)的支持了REST風(fēng)格的架構(gòu)設(shè)計(jì)

所涉及的注解

  1. @RequestMapping
  2. @PathVariable
  3. @ResponseBody
package cn.itcast.mybatis.controller;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
 
import cn.itcast.mybatis.pojo.User;
import cn.itcast.mybatis.service.NewUserService;
 
@RequestMapping("restful/user")
@Controller
public class RestUserController {
 
    @Autowired
    private NewUserService newUserService;
 
    /**
     * 根據(jù)用戶id查詢用戶數(shù)據(jù)
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<User> queryUserById(@PathVariable("id") Long id) {
        try {
            User user = this.newUserService.queryUserById(id);
            if (null == user) {
                // 資源不存在,響應(yīng)404
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            // 200
            // return ResponseEntity.status(HttpStatus.OK).body(user);
            return ResponseEntity.ok(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
 
    /**
     * 新增用戶
     * 
     * @param user
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Void> saveUser(User user) {
        try {
            this.newUserService.saveUser(user);
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
 
    /**
     * 更新用戶資源
     * 
     * @param user
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity<Void> updateUser(User user) {
        try {
            this.newUserService.updateUser(user);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
 
    /**
     * 刪除用戶資源
     * 
     * @param user
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public ResponseEntity<Void> deleteUser(@RequestParam(value = "id", defaultValue = "0") Long id) {
        try {
            if (id.intValue() == 0) {
                // 請(qǐng)求參數(shù)有誤
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            this.newUserService.deleteUserById(id);
            // 204
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
}

7.HTTP相應(yīng)狀態(tài)碼

code HTTP operation Body Contents Description
200 GET,PUT 資源 操作成功
201 POST 資源,元數(shù)據(jù) 對(duì)象創(chuàng)建成功
202 POST,PUT,DELETE,PATCH N/A 請(qǐng)求已經(jīng)被接受
204 GET N/A 操作已經(jīng)執(zhí)行成功,但是沒有返回?cái)?shù)據(jù)
301 GET link 資源已被移除
303 GET link 重定向
304 GET N/A 資源沒有被修改
400 GET,POST,PUT,DELETE,PATCH 錯(cuò)誤提示(消息) 參數(shù)列表錯(cuò)誤(缺少,格式不匹配)
401 GET,POST,PUT,DELETE,PATCH 錯(cuò)誤提示(消息) 未授權(quán)
403 GET,POST,PUT,DELETE,PATCH 錯(cuò)誤提示(消息) 訪問(wèn)受限,授權(quán)過(guò)期
404 GET,POST,PUT,DELETE,PATCH 錯(cuò)誤提示(消息) 資源,服務(wù)未找到
405 GET,POST,PUT,DELETE,PATCH 錯(cuò)誤提示(消息) 不允許的http方法
409 GET,POST,PUT,DELETE,PATCH 錯(cuò)誤提示(消息) 資源沖突,或者資源被鎖定
415 GET,POST,PUT,DELETE,PATCH 錯(cuò)誤提示(消息) 不支持的數(shù)據(jù)(媒體)類型
429 GET,POST,PUT,DELETE,PATCH 錯(cuò)誤提示(消息) 請(qǐng)求過(guò)多被限制
500 GET,POST,PUT,DELETE,PATCH 錯(cuò)誤提示(消息) 系統(tǒng)內(nèi)部錯(cuò)誤
501 GET,POST,PUT,DELETE,PATCH 錯(cuò)誤提示(消息) 接口未實(shí)現(xiàn)

原文地址:https://blog.csdn.net/chenxiaochan/article/details/73716617

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,578評(píng)論 19 139
  • 筆記 RESTful架構(gòu)風(fēng)格概述 RESTful架構(gòu)風(fēng)格 RESTful架構(gòu)風(fēng)格最初由Roy T. Fieldin...
    plutoese閱讀 12,975評(píng)論 3 58
  • 一說(shuō)到REST,我想大家的第一反應(yīng)就是“啊,就是那種前后臺(tái)通信方式?!钡窃谝笤敿?xì)講述它所提出的各個(gè)約束,以及如...
    時(shí)待吾閱讀 3,601評(píng)論 0 19
  • 原文轉(zhuǎn)自前端路上,轉(zhuǎn)載請(qǐng)注明出處:http://refined-x.com/2017/09/22/RESTful學(xué)...
    tower1229閱讀 375評(píng)論 1 5
  • 那天上英文課后,可愛的凱瑟琳老師給每個(gè)當(dāng)了媽媽的學(xué)生發(fā)了朵玫瑰,祝我們母親節(jié)快樂,感動(dòng)!不過(guò)一個(gè)人拿著一朵玫瑰走大...
    蘭知雪閱讀 442評(píng)論 0 0

友情鏈接更多精彩內(nèi)容