版權聲明:作者原創,轉載請註明出處。
本系列文章目錄地址:http://blog.csdn.net/u011961421/article/details/79416510
簡介
REST:英文representational state transfer直譯爲表現層狀態轉移,或者表述性狀態轉移;Rest是web服務的一種架構風格,一種設計風格,是一種思想;同時Rest不是針對某一種編程語言的。
以webService爲例通俗解釋。
非Rest設計,以往我們都會這麼寫:
http://localhost:8080/admin/getUser (查詢用戶)
http://localhost:8080/admin/addUser (新增用戶)
http://localhost:8080/admin/updateUser (更新用戶)
http://localhost:8080/admin/deleteUser (刪除用戶)
總結:以不同的URL(主要爲使用動詞)進行不同的操作。
Rest架構:
GET http://localhost:8080/admin/user (查詢用戶)
POST http://localhost:8080/admin/user (新增用戶)
PUT http://localhost:8080/admin/user (更新用戶)
DELETE http://localhost:8080/admin/user (刪除用戶)
總結:URL只指定資源,以HTTP方法動詞進行不同的操作。用HTTP STATUS/CODE定義操作結果。
Restful:遵守了rest風格的web服務便可稱爲Restful。
爲什麼需要Restful?
- URL具有很強可讀性的,具有自描述性
- 規範化請求過程和返回結果
- 資源描述與視圖的鬆耦合
- 可提供OpenAPI,便於第三方系統集成,提高互操作性
- 提供無狀態的服務接口,降低複雜度,可提高應用的水平擴展性
實戰
在之前SpringBoot學習系列的工程基礎上,添加Restful API。Restful的核心在controller,以用戶的增刪改查爲例,代碼如下
package com.pf.org.cms.web;
import com.alibaba.fastjson.JSONObject;
import com.pf.org.cms.common.IConstants;
import com.pf.org.cms.entity.JsonBean;
import com.pf.org.cms.entity.UserInfo;
import com.pf.org.cms.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;
import java.util.Map;
/**
* @Auther: pf
* @Date: 2018/2/27 19:52
* @Description:
*/
@RestController
@RequestMapping(value = "/admin")
public class AdminController {
@Autowired
UserService userService;
@ApiOperation(value = "getUser", notes = "管理員接口獲取用戶")
@RequestMapping(method = RequestMethod.GET, value = "/user")
public String getUser(@RequestParam long id) {
JsonBean reData = new JsonBean();
Map user = userService.getUserInfo(id);
reData.setStatus(IConstants.RESULT_INT_SUCCESS);
reData.setMessage("查詢成功");
reData.setData(user);
return JSONObject.toJSONString(reData);
}
@ApiOperation(value = "addUser", notes = "管理員接口新增用戶")
@RequestMapping(method = RequestMethod.POST, value = "/user")
public String addUser(@RequestParam String user) {
JsonBean reData = new JsonBean();
UserInfo userInfo = (UserInfo) JSONObject.parseObject(user, UserInfo.class);
if (userService.addUserInfo(userInfo)) {
reData.setStatus(IConstants.RESULT_INT_SUCCESS);
reData.setMessage("新增成功");
} else {
reData.setStatus(IConstants.RESULT_INT_ERROR);
reData.setMessage("新增失敗");
}
return JSONObject.toJSONString(reData);
}
@ApiOperation(value = "updateUser", notes = "管理員接口更新用戶")
@RequestMapping(method = RequestMethod.PUT, value = "/user")
public String updateUser(@RequestParam String user) {
JsonBean reData = new JsonBean();
UserInfo userInfo = (UserInfo) JSONObject.parseObject(user, UserInfo.class);
if (userService.updateUserInfo(userInfo)) {
reData.setStatus(IConstants.RESULT_INT_SUCCESS);
reData.setMessage("更新成功");
} else {
reData.setStatus(IConstants.RESULT_INT_ERROR);
reData.setMessage("更新失敗");
}
return JSONObject.toJSONString(reData);
}
@ApiOperation(value = "delUser", notes = "管理員接口刪除用戶")
@RequestMapping(method = RequestMethod.DELETE, value = "/user")
public String delUser(@RequestParam long id) {
JsonBean reData = new JsonBean();
if (userService.deleteUserInfo(id)) {
reData.setStatus(IConstants.RESULT_INT_SUCCESS);
reData.setMessage("刪除成功");
} else {
reData.setStatus(IConstants.RESULT_INT_ERROR);
reData.setMessage("刪除失敗");
}
return JSONObject.toJSONString(reData);
}
}
@RestController作用相當於@ResponseBody + @Controller
這裏的在線API工具使用的是SpringBoot學習(六)中介紹的swagger,@ApiOperation爲swagger中的註解。
UserService這裏不做介紹,即爲基礎的增刪改查服務,運行工程,訪問swagger-ui.html頁面後結果爲:
根據自己的測試數據測試一下查詢接口:
也可以再測試一下新增用戶的接口
最後,Restful的實現並不複雜,最重要的是需要理解Restful的思想,並且在架構設計好後更加規範的編碼執行。本文及本系列項目工程地址:https://github.com/15651037763/cms