Rest簡介
- REST是英文representational state transfer(表象性狀態轉變)或者表述性狀態轉移。
- Rest是web服務的一種架構風格。
- 使用HTTP,URI,XML,JSON,HTML等廣泛流行的標準和協議。
- 輕量級,跨平臺,跨語言的架構設計。
- 它是一種設計風格,不是一種標準,是一種思想。
Rest架構的主要原則
- 網絡上的所有事物都被抽象為資源
- 每個資源都有一個唯一的資源標識符
- 同一個資源具有多種表現形式(xml,json等)
- 對資源的各種操作不會改變資源標識符
- 所有的操作都是無狀態的
- 符合REST原則的架構方式即可稱為RESTful
什么是Restful
- 對應的中文是rest式的
- Restful web service是一種常見的rest的應用,是遵守了rest風格的web服務
- rest式的web服務是一種ROA(The Resource-Oriented Architecture)(面向資源的架構)
為什么會出現Restful
在Restful之前的操作
- http://127.0.0.1/user/query GET 根據用戶id查詢用戶數據
- http://127.0.0.1/user/save POST 新增用戶
- http://127.0.0.1/user/update POST 修改用戶信息
- http://127.0.0.1/user/delete GET/POST 刪除用戶信息
Restful用法
- http://127.0.0.1/user/1 GET 根據用戶id查詢用戶數據
- http://127.0.0.1/user ?POST 新增用戶
- http://127.0.0.1/user ?PUT 修改用戶信息
- http://127.0.0.1/user ?DELETE 刪除用戶信息
常用的HTTP動詞有下面五個(括號里是對應的SQL命令)
- GET(SELECT):從服務器取出資源(一項或多項)。
- POST(CREATE):在服務器新建一個資源。
- PUT(UPDATE):在服務器更新資源(客戶端提供改變后的完整資源)。
- PATCH(UPDATE):在服務器更新資源(客戶端提供改變的屬性)。
- DELETE(DELETE):從服務器刪除資源。
如何使用
http方法 | 資源操作 | 冪等 | 安全 |
---|---|---|---|
GET | SELECT | 是 | 否 |
POST | INSERT | 否 | 否 |
PUT | UPDATE | 是 | 否 |
DELETE | DELETE | 是 | 否 |
冪等性:對同一REST接口的多次訪問,得到的資源狀態是相同的。
安全性:對該REST接口訪問,不會使服務器端資源的狀態發生改變。
##6.SpringMVC原生態的支持了REST風格的架構設計
###所涉及的注解
- @RequestMapping
- @PathVariable
- @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 {@Autowiredprivate NewUserService newUserService;/*** 根據用戶id查詢用戶數據* * @param id* @return*/@RequestMapping(value = "{id}", method = RequestMethod.GET)@ResponseBodypublic ResponseEntity<User> queryUserById(@PathVariable("id") Long id) {try {User user = this.newUserService.queryUserById(id);if (null == user) {// 資源不存在,響應404return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);}// 200// return ResponseEntity.status(HttpStatus.OK).body(user);return ResponseEntity.ok(user);} catch (Exception e) {e.printStackTrace();}// 500return 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 blocke.printStackTrace();}// 500return 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();}// 500return 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) {// 請求參數有誤return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();}this.newUserService.deleteUserById(id);// 204return ResponseEntity.status(HttpStatus.NO_CONTENT).build();} catch (Exception e) {e.printStackTrace();}// 500return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);}
}
##7.HTTP相應狀態碼
code | HTTP operation | Body Contents | Description |
---|---|---|---|
200 | GET,PUT | 資源 | 操作成功 |
201 | POST | 資源,元數據 | 對象創建成功 |
202 | POST,PUT,DELETE,PATCH | N/A | 請求已經被接受 |
204 | GET | N/A | 操作已經執行成功,但是沒有返回數據 |
301 | GET | link | 資源已被移除 |
303 | GET | link | 重定向 |
304 | GET | N/A | 資源沒有被修改 |
400 | GET,POST,PUT,DELETE,PATCH | 錯誤提示(消息) | 參數列表錯誤(缺少,格式不匹配) |
401 | GET,POST,PUT,DELETE,PATCH | 錯誤提示(消息) | 未授權 |
403 | GET,POST,PUT,DELETE,PATCH | 錯誤提示(消息) | 訪問受限,授權過期 |
404 | GET,POST,PUT,DELETE,PATCH | 錯誤提示(消息) | 資源,服務未找到 |
405 | GET,POST,PUT,DELETE,PATCH | 錯誤提示(消息) | 不允許的http方法 |
409 | GET,POST,PUT,DELETE,PATCH | 錯誤提示(消息) | 資源沖突,或者資源被鎖定 |
415 | GET,POST,PUT,DELETE,PATCH | 錯誤提示(消息) | 不支持的數據(媒體)類型 |
429 | GET,POST,PUT,DELETE,PATCH | 錯誤提示(消息) | 請求過多被限制 |
500 | GET,POST,PUT,DELETE,PATCH | 錯誤提示(消息) | 系統內部錯誤 |
501 | GET,POST,PUT,DELETE,PATCH | 錯誤提示(消息) | 接口未實現 |
原文地址:https://blog.csdn.net/chenxiaochan/article/details/73716617