当前位置:网站首页>基於MVC的RESTFul風格API實戰
基於MVC的RESTFul風格API實戰
2020-11-06 20:10:00 【itread01】
`PUT/users{id}`[^建立客戶端維護主鍵資訊的資源] | | 刪除 | `DELETE/users/{id}` | | 修改/更新 | `PUT/users/{id}` | | 查詢全部 | `GET/users` | | 主鍵查詢 | `GET/users/{id}`
`GET/users?id=26` | | 分頁作用域查詢 | `GET/users?start=0&size=10`
`GET/users?07,2019-07,2020` | 可以看到通過這個`RESTAPI`都是通過對==同一個資源==的操作,所不同的就是通過不同的==HTTP方法==來實現對資源不同的處理。 #### 2.`MVC`對`REST`的支援 ##### 1.1主要通過註解來實現 * `@Controller`聲名一個處理請求的控制器 * `@RequestMapping`請求對映地址,它存在幾個子註解對於實現`REST`風格來說更加具有==語義性== * `@GETMapping` ==GET請求== * `@PUTMapping` ==PUT請求== * `@POSTMapping` ==POST請求== * `@DELETEMapping` ==DELETE請求== * `@ResponseBody` 將響應內容轉換為`JSON`格式 * `@RequestBody` 請求內容轉換為`JSON`格式 * `@PathVariable("id")`用於繫結一個引數 * `@RESTController` 等同於`@Controller`+`@ResponseBody`在類上寫了這個註解,標識這個類的所有方法只==返回資料==,而不進行==檢視跳轉== ##### 1.2返回`HTTP`狀態碼 **`REST`風格`API`一個最鮮明的特點通過返回對應的`HTTPStatus`來判斷客戶端的操作是否完成** ==下面是spring中關於`Http`狀態碼描述的列舉類,本文列舉了常見的狀態碼==(讀者若對此感興趣可以檢視`HttpStatus`原始碼) ~~~java public enum HttpStatus{ OK(200, "OK"),//用於伺服器有實體響應 CREATED(201, "Created"),//建立了新實體,響應該實體 NO_CONTENT(204, "No Content"),//伺服器正常響應,但無實體響應 BAD_REQUEST(400, "Bad Request"),//客戶端請求語法錯誤 NOT_FOUND(404, "Not Found"),//目標資源不存在 INTERNAL_SERVER_ERROR(500, "Internal Server Error"),//伺服器內部錯誤 NOT_IMPLEMENTED(501, "Not Implemented"),//伺服器不支援當前請求 } ~~~ Spring返回狀態碼是通過`@ResponseStatus`註解或者`ResponseEntity `類實現的。 ==`@ResponseStatus`方式== ~~~java @GetMapping(path = "/user/{id}" , produces = "application/json;charset=utf-8") @ResponseStatus(HttpStatus.OK) public User findUserById(@PathVariable("id")Integer id){ User user = userService.findUserById(id); return user ; } ~~~ ==`ResponseEntity `==方式 ~~~java @GetMapping(produces = "application/json;charset=utf-8") public ResponseEntity
-
> findAll(){ List
-
>(users , HttpStatus.OK); } ~~~ ##### 1.3由於`MVC`預設不支援`PUT`和`DELETE`方法,所以需要手動開啟 *在`tomcat`伺服器的`web.xml`檔案中開啟一下配置* ~~~xml
-
> findAll(){ List
-
>(users , HttpStatus.OK); } /**、 * 根據ID查詢 * @param id * @return */ @GetMapping(path = "/{id}" , produces = "application/json;charset=utf-8") @ResponseStatus(HttpStatus.OK) public User findUserById(@PathVariable("id")Integer id){ User user = userService.findUserById(id); return user ; } /** * 增加一個使用者 * 返回該使用者 */ @PostMapping(produces = "application/json;charset=utf-8") @ResponseStatus(HttpStatus.CREATED) public User addUser(@RequestBody User user){ User newUser = userService.addUser(user); return newUser ; } /** * 更新 * @param user */ @PutMapping(path = "/{id}" ,produces = "application/json;charset=utf-8") public ResponseEntity
版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
https://www.itread01.com/content/1604650082.html
边栏推荐
- 链表的常见算法总结
- Listening to silent words: hand in hand teaching you sign language recognition with modelarts
- 高级 Vue 组件模式 (3)
- 【Flutter 實戰】pubspec.yaml 配置檔案詳解
- DRF JWT authentication module and self customization
- Details of dapr implementing distributed stateful service
- 人工智能学什么课程?它将替代人类工作?
- Using tensorflow to forecast the rental price of airbnb in New York City
- 6.8 multipartresolver file upload parser (in-depth analysis of SSM and project practice)
- python jieba分词(结巴分词)、提取词,加载词,修改词频,定义词库
猜你喜欢
随机推荐
神经网络简史
nlp模型-bert从入门到精通(一)
給萌新HTML5 入門指南(二)
JVM内存区域与垃圾回收
GUI 引擎评价指标
How to demote a domain controller in Windows Server 2012 and later
字符串的常见算法总结
Outlier detection based on RNN self encoder
python 下载模块加速实现记录
7.2.1 cache configuration of static resources
6.9.1 flashmapmanager initialization (flashmapmanager redirection Management) - SSM in depth analysis and project practice
恕我直言,我也是才知道ElasticSearch条件更新是这么玩的
普通算法面试已经Out啦!机器学习算法面试出炉 - kdnuggets
从零学习人工智能,开启职业规划之路!
C++和C++程序员快要被市场淘汰了
【QT】 QThread部分原始碼淺析
7.3.1 file upload and zero XML registration interceptor
Basic principle and application of iptables
7.3.2 File Download & big file download
读取、创建和运行多个文件的3个Python技巧






