1.Rest簡(jiǎn)介
- REST是英文representational state transfer(表象性狀態(tài)轉(zhuǎn)變)或者表述性狀態(tài)轉(zhuǎn)移。
- Rest是web服務(wù)的一種架構(gòu)風(fēng)格。
- 使用HTTP,URI,XML,JSON,HTML等廣泛流行的標(biāo)準(zhǔn)和協(xié)議。
- 輕量級(jí),跨平臺(tái),跨語(yǔ)言的架構(gòu)設(shè)計(jì)。
- 它是一種設(shè)計(jì)風(fēng)格,不是一種標(biāo)準(zhǔn),是一種思想。
2.Rest架構(gòu)的主要原則
- 網(wǎng)絡(luò)上的所有事物都被抽象為資源
- 每個(gè)資源都有一個(gè)唯一的資源標(biāo)識(shí)符
- 同一個(gè)資源具有多種表現(xiàn)形式(xml,json等)
- 對(duì)資源的各種操作不會(huì)改變資源標(biāo)識(shí)符
- 所有的操作都是無(wú)狀態(tài)的
- 符合REST原則的架構(gòu)方式即可稱為RESTful
3.什么是Restful
- 對(duì)應(yīng)的中文是rest式的
- Restful web service是一種常見的rest的應(yīng)用,是遵守了rest風(fēng)格的web服務(wù)
- rest式的web服務(wù)是一種ROA(The Resource-Oriented Architecture)(面向資源的架構(gòu))
4.為什么會(huì)出現(xiàn)Restful
在Restful之前的操作
- http://127.0.0.1/user/query GET 根據(jù)用戶id查詢用戶數(shù)據(jù)
- 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 根據(jù)用戶id查詢用戶數(shù)據(jù)
- http://127.0.0.1/user POST 新增用戶
- http://127.0.0.1/user PUT 修改用戶信息
- http://127.0.0.1/user DELETE 刪除用戶信息
常用的HTTP動(dòng)詞有下面五個(gè)(括號(hào)里是對(duì)應(yīng)的SQL命令)
- GET(SELECT):從服務(wù)器取出資源(一項(xiàng)或多項(xiàng))。
- POST(CREATE):在服務(wù)器新建一個(gè)資源。
- PUT(UPDATE):在服務(wù)器更新資源(客戶端提供改變后的完整資源)。
- PATCH(UPDATE):在服務(wù)器更新資源(客戶端提供改變的屬性)。
- 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ì)
所涉及的注解
- @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 {
@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