当前位置:网站首页>基於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
边栏推荐
猜你喜欢
随机推荐
微信小程序:防止多次点击跳转(函数节流)
业内首发车道级导航背后——详解高精定位技术演进与场景应用
自然语言处理-搜索中常用的bm25
从零学习人工智能,开启职业规划之路!
X Window System介紹
怎么理解Python迭代器与生成器?
(1) ASP.NET Introduction to core3.1 Ocelot
DRF JWT authentication module and self customization
通过深层神经网络生成音乐
WeihanLi.Npoi 1.11.0/1.12.0 Release Notes
文本去重的技术方案讨论(一)
我们编写 React 组件的最佳实践
【Flutter 實戰】pubspec.yaml 配置檔案詳解
5.4 静态资源映射 -《SSM深入解析与项目实战》
什么是无副作用的函数方法?如何取名? - Mario
keras model.compile损失函数与优化器
Using tensorflow to forecast the rental price of airbnb in New York City
2018个人年度工作总结与2019工作计划(互联网)
【效能優化】納尼?記憶體又溢位了?!是時候總結一波了!!
简直骚操作,ThreadLocal还能当缓存用