Restful理論與實踐

 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/1 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 刪除用戶信息

之前的操作是沒有問題的,大神認爲是有問題的,有什麼問題呢?你每次請求的接口或者地址,都在做描述,例如查詢的時候用了query,新增的時候用了save,其實完全沒有這個必要,我使用了get請求,就是查詢.使用post請求,就是新增的請求,我的意圖很明顯,完全沒有必要做描述,這就是爲什麼有了restful.

如何使用:

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

冪等性:對同一REST接口的多次訪問,得到的資源狀態是相同的。

安全性:對該REST接口訪問,不會使服務器端資源的狀態發生改變。

SpringMVC實現restful服務:

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 {
 
    @Autowired
    private NewUserService newUserService;
 
    /**
     * 根據用戶id查詢用戶數據
     * 
     * @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) {
                // 資源不存在,響應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) {
                // 請求參數有誤
                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);
    }
}


HTTP相應狀態碼:

常見:

Code Http Operation Body Contents Description
200 GET,PUT 資源 success(ok)
301 GET link 資源已被移除
404 GET,PUT,POST,DELETE,PATCH 錯誤提示(消息) 資源,服務未找到
500 GET,PUT,POST,DELETE,PATCH 錯誤提示(消息) 系統內部錯誤


總結:
restful就是舊技術,新風格.之前寫過一篇關於restful接口的博客:【Restful接口】restful接口的兩種使用方式

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章