【Restful】三分鐘完全瞭解Restful最佳實踐

轉載自:http://blog.csdn.net/chenxiaochan/article/details/73716617java

REST是英文representational state transfer(表象性狀態轉變)或者表述性狀態轉移;Rest是web服務的一種架構風格;使用HTTP,URI,XML,JSON,HTML等普遍流行的標準和協議;輕量級,跨平臺,跨語言的架構設計;它是一種設計風格,不是一種標準,是一種思想web

Rest架構的主要原則

     網絡上的全部事物都被抽象爲資源spring

    每一個資源都有一個惟一的資源標識符json

    同一個資源具備多種表現形式(xml,json等)restful

    對資源的各類操做不會改變資源標識符網絡

    全部的操做都是無狀態的mybatis

    符合REST原則的架構方式便可稱爲RESTful架構

什麼是Restful:

        對應的中文是rest式的;Restful web service是一種常見的rest的應用,是遵照了rest風格的web服務;rest式的web服務是一種ROA(The Resource-Oriented Architecture)(面向資源的架構).app

爲何會出現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 刪除用戶信息post

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

如何使用:

 

SpringMVC實現restful服務:

SpringMVC原生態的支持了REST風格的架構設計

所涉及到的註解:

--@RequestMapping

---@PathVariable

---@ResponseBody

 

[java]  view plain  copy
 
  1. package cn.itcast.mybatis.controller;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.http.HttpStatus;  
  5. import org.springframework.http.ResponseEntity;  
  6. import org.springframework.stereotype.Controller;  
  7. import org.springframework.web.bind.annotation.PathVariable;  
  8. import org.springframework.web.bind.annotation.RequestMapping;  
  9. import org.springframework.web.bind.annotation.RequestMethod;  
  10. import org.springframework.web.bind.annotation.RequestParam;  
  11. import org.springframework.web.bind.annotation.ResponseBody;  
  12.   
  13. import cn.itcast.mybatis.pojo.User;  
  14. import cn.itcast.mybatis.service.NewUserService;  
  15.   
  16. @RequestMapping("restful/user")  
  17. @Controller  
  18. public class RestUserController {  
  19.   
  20.     @Autowired  
  21.     private NewUserService newUserService;  
  22.   
  23.     /** 
  24.      * 根據用戶id查詢用戶數據 
  25.      *  
  26.      * @param id 
  27.      * @return 
  28.      */  
  29.     @RequestMapping(value = "{id}", method = RequestMethod.GET)  
  30.     @ResponseBody  
  31.     public ResponseEntity<User> queryUserById(@PathVariable("id") Long id) {  
  32.         try {  
  33.             User user = this.newUserService.queryUserById(id);  
  34.             if (null == user) {  
  35.                 // 資源不存在,響應404  
  36.                 return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);  
  37.             }  
  38.             // 200  
  39.             // return ResponseEntity.status(HttpStatus.OK).body(user);  
  40.             return ResponseEntity.ok(user);  
  41.         } catch (Exception e) {  
  42.             e.printStackTrace();  
  43.         }  
  44.         // 500  
  45.         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);  
  46.     }  
  47.   
  48.     /** 
  49.      * 新增用戶 
  50.      *  
  51.      * @param user 
  52.      * @return 
  53.      */  
  54.     @RequestMapping(method = RequestMethod.POST)  
  55.     public ResponseEntity<Void> saveUser(User user) {  
  56.         try {  
  57.             this.newUserService.saveUser(user);  
  58.             return ResponseEntity.status(HttpStatus.CREATED).build();  
  59.         } catch (Exception e) {  
  60.             // TODO Auto-generated catch block  
  61.             e.printStackTrace();  
  62.         }  
  63.         // 500  
  64.         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);  
  65.     }  
  66.   
  67.     /** 
  68.      * 更新用戶資源 
  69.      *  
  70.      * @param user 
  71.      * @return 
  72.      */  
  73.     @RequestMapping(method = RequestMethod.PUT)  
  74.     public ResponseEntity<Void> updateUser(User user) {  
  75.         try {  
  76.             this.newUserService.updateUser(user);  
  77.             return ResponseEntity.status(HttpStatus.NO_CONTENT).build();  
  78.         } catch (Exception e) {  
  79.             e.printStackTrace();  
  80.         }  
  81.         // 500  
  82.         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);  
  83.     }  
  84.   
  85.     /** 
  86.      * 刪除用戶資源 
  87.      *  
  88.      * @param user 
  89.      * @return 
  90.      */  
  91.     @RequestMapping(method = RequestMethod.DELETE)  
  92.     public ResponseEntity<Void> deleteUser(@RequestParam(value = "id", defaultValue = "0") Long id) {  
  93.         try {  
  94.             if (id.intValue() == 0) {  
  95.                 // 請求參數有誤  
  96.                 return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();  
  97.             }  
  98.             this.newUserService.deleteUserById(id);  
  99.             // 204  
  100.             return ResponseEntity.status(HttpStatus.NO_CONTENT).build();  
  101.         } catch (Exception e) {  
  102.             e.printStackTrace();  
  103.         }  
  104.         // 500  
  105.         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);  
  106.     }  
  107. }  



 

HTTP相應狀態碼:

 

總結:

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

相關文章
相關標籤/搜索