最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談Spring Boot 開發(fā)REST接口最佳實踐

 更新時間:2018年01月12日 10:05:38   作者:固安李慶海  
這篇文章主要介紹了淺談Spring Boot 開發(fā)REST接口最佳實踐,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文介紹了Spring Boot 開發(fā)REST接口最佳實踐,分享給大家,具體如下:

HTTP動詞與SQL命令對應

GET

從服務器獲取資源,可一個或者多個,對應SQL命令中的SELECT
GET /users
獲取服務器上的所有的用戶信息
GET /users/ID
獲取指定ID的用戶信息

POST

在服務器上創(chuàng)建一個新資源,對應SQL命令中的CREATE
POST /users
創(chuàng)建一個新的用戶

PUT

在服務器上更新一個資源,客戶端提供改變后的完整資源,對應SQL命令中的UPDATE
PUT /users/ID
更新指定ID的用戶的全部信息

DELETE

從服務器上刪除一個資源,對應SQL命令中的DELETE
DELETE /users/ID
刪除指定ID的用戶信息

PATCH

在服務器更新一個資源的部分屬性,對應SQL命令中的UPDATE
PATCH /users/ID
更新指定ID的用戶的某個屬性

URL中的約定

URL中名詞使用復數(shù)形式

URL中的名稱是使用單數(shù)還是使用復數(shù)的問題,爭議由來已久。URL中的名詞一般對應數(shù)據(jù)庫中的表,表中存儲的是同類數(shù)據(jù), 在實踐中我是強制使用復數(shù)形式 ,看上去更舒服些。

/users
/users/1
/roles
/roles/1

至于一些不規(guī)則的、不可數(shù)的名詞就見仁見智吧。

/heroes
/heroes/1
/people
/people/1
/foots
/foots/1
/feet
/feet/1

版本

講版本號加入到URL中以應對不兼容的和破壞性的更改。發(fā)布新API時,客戶端可以自如的遷移到新API,不會因調(diào)用完全不同的新API而陷入窘境。使用直觀的“V”前綴來表示后面的數(shù)字是版本號,不需要次級版本號,不應該頻繁的發(fā)布API版本。

/edu/v1/users
/edu/v1/roles

對可選的、復雜的參數(shù)使用查詢字符串

為了讓URL更小、更簡潔,為資源設(shè)置一個基本URL,講可選的、復雜的參數(shù)用查詢字符串表示。

/edu/v1/users?enabled=1&roleid=1

提供分頁信息

一次性返回數(shù)據(jù)庫中的所有的資源不是一個好主意,因此需要提供分頁機制。通常使用數(shù)據(jù)庫中眾所周知的參數(shù)offset和limit

/edu/v1/users?enabled=1&offset=1&limit=15

如果客戶端沒有傳遞這些參數(shù),則應使用默認值,通常offset=0,limit=10。

非資源請求使用動詞

有時API調(diào)用并不涉及資源,在這種情況下,服務器執(zhí)行一個操作病將結(jié)果返回給客戶端。

/edu/v1/calc?p=100

考慮特定資源和跨資源搜索

提供對特定止緣的搜索很容易,只需要使用相應的資源集合,并將搜索字符串附加到查詢參數(shù)中即可。

/edu/v1/users?username=李慶海

如果需要對所有資源提供全局搜索,則需要使用其他方法。

/edu/v1/search?key=李慶海

響應結(jié)果

使用小駝峰命名法作為屬性標識符

通常,RESTful Web服務將被JavaScript編寫的客戶端使用??蛻舳藭SON響應轉(zhuǎn)換為JavaScript對象,然后調(diào)用其屬性。因此,最好遵循JavaScript代碼通用規(guī)范。

person.year_of_birth // 不推薦,違反JavaScript代碼通用規(guī)范 
person.YearOfBirth // 不推薦,JavaScript構(gòu)造方法命名 
person.yearOfBirth // 推薦

提供分頁信息

返回結(jié)果比較多時,應提供分頁信息。

{ 
 "page": 0, 
 "size": 10, 
 "total": 3465, 
 "obj": [ 
  
 ] 
}

Spring MVC開發(fā)REST接口

常用注解

@RestController

@RestController是@ResponseBody和@Controller的組合注解。

@RequestMapping

此注解即可以作用在控制器的某個方法上,也可以作用在此控制器類上。當控制器在類級別上添加@RequestMapping注解時,這個注解會應用到控制器的所有處理器方法上。處理器方法上的@RequestMapping注解會對類級別上的@RequestMapping的聲明進行補充。

@PostMapping

組合注解,是@RequestMapping(method =RequestMethod.POST)的縮寫。

@PutMapping

組合注解,是@RequestMapping(method = RequestMethod.PUT)的縮寫。

@PatchMapping

組合注解,是@RequestMapping(method = RequestMethod.PATCH)的縮寫。

@DeleteMapping

組合注解,是@RequestMapping(method = RequestMethod.DELETE)的縮寫。

@GetMapping

組合注解,是@RequestMapping(method = RequestMethod.GET)的縮寫。

@PathVariable

獲取url中的數(shù)據(jù)。

@RequestParam

獲取請求參數(shù)的值。

REST接口及Swagger 編寫API文檔示例

關(guān)于Swagger的使用可參考Spring Boot 項目中使用Swagger2 。方法體中的代碼不重要,重要的是方法的簽名以及與HTTP動詞的映射。

import java.util.Date;
import javax.persistence.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import cn.com.infcn.jianshu.Service.UserService;
import cn.com.infcn.jianshu.exception.BizException;
import cn.com.infcn.jianshu.exception.LoginNameOrPasswordErrorException;
import cn.com.infcn.jianshu.exception.ResourceExistsException;
import cn.com.infcn.jianshu.model.User;
import cn.com.infcn.jianshu.util.JsonResult;
import io.swagger.annotations.Api; 
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 系統(tǒng)用戶Controller
 * 
 * @author 李慶海
 *
 */
@Api(value = "系統(tǒng)用戶接口", tags = "系統(tǒng)管理")
@RestController
@RequestMapping("/v3/edu/users")
public class UserController {

 @Autowired
 private UserService userService;

 /**
  * 添加用戶,注冊
  * 
  * @param loginName
  *   登錄賬號
  * @param userName
  *   用戶名稱
  * @param password
  *   登錄密碼
  * @param roleId
  *   用戶角色
  * @return
  * @throws ResourceExistsException
  */
 @ApiOperation(value = "添加用戶")
 @PostMapping("/")
 public JsonResult create(
   @ApiParam(name = "loginName", value = "登錄賬號", required = true) @RequestParam(required = true) @RequestBody String loginName,
   @ApiParam(name = "userName", value = "用戶名稱", required = true) @RequestParam(required = true) @RequestBody String userName,
   @ApiParam(name = "password", value = "登錄密碼", required = true) @RequestParam(required = true) @RequestBody String password,
   @ApiParam(name = "roleId", value = "用戶角色編號", required = true) @RequestParam(required = true) @RequestBody String roleId)
   throws ResourceExistsException {
  boolean exists = this.userService.exists(loginName);
  if (exists) {
   throw new ResourceExistsException(loginName);
  }
  User user = userService.create(loginName, password, userName, roleId);
  return JsonResult.success(user);
 }

 /**
  * 用戶憑借登錄賬號和登錄密碼進行登錄
  * 
  * @param loginName
  *   登錄賬號
  * @param password
  *   登錄密碼
  * @throws EntityNotFoundException
  */
 @ApiOperation(value = "根據(jù)用戶編號查詢用戶信息")
 @GetMapping("/login")
 public JsonResult login(
   @ApiParam(name = "loginName", value = "登錄賬號", required = true) @RequestParam(required = true) String loginName,
   @ApiParam(name = "password", value = "登錄密碼", required = true) @RequestParam(required = true) String password)
   throws LoginNameOrPasswordErrorException {
  User user = this.userService.login(loginName, password);
  if (null == user) {
   throw new LoginNameOrPasswordErrorException();
  }
  return JsonResult.success(user);
 }

 /**
  * 根據(jù)用戶編號查詢用戶信息
  * 
  * @param id
  *   用戶編號
  * @throws EntityNotFoundException
  */
 @ApiOperation(value = "根據(jù)用戶編號查詢用戶信息")
 @GetMapping("/{id}")
 public JsonResult read(
   @ApiParam(name = "id", value = "用戶編號,主鍵", required = true) @PathVariable(required = true) String id)
   throws EntityNotFoundException {
  User user = this.userService.getOne(id);
  return JsonResult.success(user);
 }

 /**
  * 賬戶注銷,不刪除用戶的數(shù)據(jù)
  * 
  * @param userId
  *   用戶編號
  * @return
  */
 @ApiOperation(value = "注銷賬戶")
 @PatchMapping("/{id}")
 public JsonResult cancel(
   @ApiParam(name = "id", value = "用戶編號,主鍵", required = true) @PathVariable(required = true) String id)
   throws EntityNotFoundException {
  this.userService.cancel(id);
  return JsonResult.success();
 }

 /**
  * 重置密碼
  * 
  * @param id
  *   用戶編號
  * @param password
  *   新登錄密碼
  * @return
  */
 @ApiOperation(value = "重置密碼")
 @PatchMapping("/")
 public JsonResult updatePassword(
   @ApiParam(name = "id", value = "用戶編號,主鍵", required = true) @RequestParam(required = true) String id,
   @ApiParam(name = "password", value = "新登錄密碼", required = true) @RequestParam(required = true) String password) {
  this.userService.updatePassword(id, password);
  return JsonResult.success();
 }

 /**
  * 多條件組合查詢
  * 
  * @param userName
  *   用戶名稱
  * @param roleId
  *   用戶角色
  * @param start
  *   開始日期
  * @param end
  *   結(jié)束日期
  * @param page
  *   分頁,從0開始
  * @param size
  *   每頁的行數(shù),默認10
  * @return
  * @throws BizException
  */
 @ApiOperation(value = "用戶信息查詢")
 @GetMapping("/")
 public JsonResult query(
   @ApiParam(name = "userName", value = "用戶名稱,查詢關(guān)鍵詞", required = false) @RequestParam(required = false) String userName,
   @ApiParam(name = "roleId", value = "用戶角色編號", required = false) @RequestParam(required = false) String roleId,
   @ApiParam(name = "start", value = "用戶角色編號", required = false) @RequestParam(required = false) Date start,
   @ApiParam(name = "end", value = "用戶角色編號", required = false) @RequestParam(required = false) Date end,
   @ApiParam(name = "page", value = "分頁,第幾頁,從1開始", defaultValue = "1", required = true) @RequestParam(defaultValue = "1", required = true) int page,
   @ApiParam(name = "size", value = "每頁的行數(shù),正整數(shù)", defaultValue = "10", required = true) @RequestParam(defaultValue = "10", required = true) int size)
   throws BizException {
  Page<User> datas = this.userService.findDatas(userName, roleId, start, end, page, size);
  if (null == datas || null == datas.getContent() || datas.getContent().isEmpty()) {
   throw new BizException("用戶不存在");
  }
  return JsonResult.success(datas);
 }

}

Swagger2接口文檔效果圖

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • IDEA 2021.1 的 Win 和 Mac 快捷鍵大全

    IDEA 2021.1 的 Win 和 Mac 快捷鍵大全

    這篇文章主要介紹了IDEA 2021.1 的 Win 和 Mac 快捷鍵大全,本文是小編給大家精心收藏的,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • IDEA2023版本創(chuàng)建Spring項目只能勾選17和21卻無法使用Java8的完美解決方案

    IDEA2023版本創(chuàng)建Spring項目只能勾選17和21卻無法使用Java8的完美解決方案

    想創(chuàng)建一個springboot的項目,本地安裝的是1.8,但是在使用Spring Initializr創(chuàng)建項目時,發(fā)現(xiàn)版本只有17和21,這篇文章主要介紹了IDEA2023版本創(chuàng)建Sping項目只能勾選17和21,卻無法使用Java8的解決方法,需要的朋友可以參考下
    2023-12-12
  • mybatis單元測試過程(無需啟動容器)

    mybatis單元測試過程(無需啟動容器)

    在MyBatis中,單元測試無需啟動容器即可進行,主要涉及Configuration類、Executor接口及其實現(xiàn)類,以及XMLMapperBuilder的作用,Configuration類是配置的承載者,負責初始化并解析配置文件,Executor接口及其實現(xiàn)類
    2024-09-09
  • Java超詳細講解如何生成隨機整數(shù)

    Java超詳細講解如何生成隨機整數(shù)

    在?Java?中,生成隨機數(shù)的場景有很多,所以本文我們就來盤點一下?幾種生成隨機數(shù)的方式,以及它們之間的區(qū)別和每種生成方式所對應的場景
    2022-05-05
  • mybatis中association標簽的使用解讀

    mybatis中association標簽的使用解讀

    這篇文章主要介紹了mybatis中association標簽的使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • SpringBoot使用Redis方式

    SpringBoot使用Redis方式

    文章介紹了SpringDataRedis操作本地Redis數(shù)據(jù)庫的方法,包括Redis的基本概念、數(shù)據(jù)類型、命令以及在Java中的使用
    2024-11-11
  • 深入分析:用1K內(nèi)存實現(xiàn)高效I/O的RandomAccessFile類的詳解

    深入分析:用1K內(nèi)存實現(xiàn)高效I/O的RandomAccessFile類的詳解

    本篇文章是對用1K內(nèi)存實現(xiàn)高效I/O的RandomAccessFile類的詳細分析介紹,需要的朋友參考下
    2013-05-05
  • Spring中@RefreshScope注解的處理方法詳解

    Spring中@RefreshScope注解的處理方法詳解

    這篇文章主要介紹了Spring中@RefreshScope注解的處理方法詳解,spring啟動時會調(diào)用ClassPathBeanDefinitionScanner.java類中的doScan()對包路徑下的所有class進行掃描,獲取bean的定義,同時對bean的@RefreshScope(@Scope的父類)進行處理,需要的朋友可以參考下
    2023-10-10
  • Spring配置文件使用占位符配置方式

    Spring配置文件使用占位符配置方式

    這篇文章主要介紹了Spring配置文件使用占位符配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Spring Cloud 請求重試機制核心代碼分析

    Spring Cloud 請求重試機制核心代碼分析

    這篇文章主要介紹了Spring Cloud 請求重試機制核心代碼分析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06

最新評論

沧源| 台山市| 台中县| 南京市| 梓潼县| 静海县| 屯昌县| 玉龙| 巴马| 图们市| 南部县| 临猗县| 左云县| 绩溪县| 都兰县| 湟源县| 九寨沟县| 新源县| 门源| 高要市| 福泉市| 福安市| 延津县| 浙江省| 峨眉山市| 沙湾县| 太湖县| 崇州市| 清原| 金阳县| 曲沃县| 德清县| 珠海市| 海口市| 孟津县| 含山县| 敦化市| 通河县| 舒城县| 万州区| 鄂托克旗|