RestFull api接口html
先後端分離開發的接口規範java
什麼是RestFull 是目錄比較流行的api設計規範web
注:restfull api規範應用場景,先後端分離的項目中spring
數據接口的現場 例如:數據庫
/users/999 獲取ID爲999的信息json
/users/list 獲取全部的用戶信息後端
/users/add 打開添加的頁面api
/users/save 新增數據springboot
/users/edit 打開修改的頁面restful
/users/save 根據表單是否有主鍵的值判斷是否有更新
/users/del/999 刪除id 爲999的信息
Restfull風格的api接口,經過不一樣的請求方式來區分不一樣的操做
get /users/999 獲取id爲999的信息
get /users 獲取全部的用戶信息
post /users 新增一條記錄
put /users 修改信息
patch /users 增量的修改
delete /users/999 刪除id爲999的信息
如何建立restfull風格的數據接口
注:springmvc對restfull風格的api有很好的支持
風格以下
package com.seecen.sc1904springboot.controller; import com.seecen.sc1904springboot.pojo.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import java.util.ArrayList; import java.util.List; /** * get /users/999 獲取id爲999的信息 * get /users 獲取全部的用戶信息 * post /users 新增一條記錄 * put /users 修改信息 * patch /users 增量的修改 * delete /users/999 刪除id爲999的信息 */ @Controller @RequestMapping("users") public class UserController { //get users/9999
@RequestMapping(value = "/{id}", method = RequestMethod.GET) @ResponseBody public User getUserById(@PathVariable("id") Integer id) { //持久化操做:根據id獲取指定記錄並返回
User user = new User(); user.setUserId(id); user.setUserName("張三"); return user; } //get /users 獲取全部的用戶信息
@RequestMapping(value = "",method = RequestMethod.GET) @ResponseBody public List<User> getAllUsers(){ List<User> list = new ArrayList<>(); User user = new User(); user.setUserId(1); user.setUserName("張三"); list.add(user); return list; } // post /users 新增一條記錄
@RequestMapping(value = "",method = RequestMethod.POST) @ResponseBody public User addNewUser(User user){ //新增一條記錄並獲取user對象
return user; } // put /users 修改信息
@RequestMapping(value = "",method = RequestMethod.PUT) @ResponseBody public User updateUser(User user){ return user; } //patch /users 增量的改
@RequestMapping(value = "",method = RequestMethod.PATCH) @ResponseBody public User patchUser(User user){ return user; } //delete /users/999
@RequestMapping(value = "/{id}",method = RequestMethod.DELETE) @ResponseBody public User del(@PathVariable("id") Integer id){ return new User(); } }
Postman測試
Swaggerui框架測試
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
2.編寫配置文件(JAVA類的方式進行配置)
Java類來管理bean對象
經過@Bean註解來管理bean對象 (必需要配置)
package com.seecen.sc1904springboot.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; //@Configuration 就個類是一個spring框架的配置文件 //spring框架的配置文件主要體現的是建立什麼bean對象
@Configuration//spring配置文件,xml, java類來體現配置信息
@EnableSwagger2 public class Swagger2 { /** * 建立API應用 * apiInfo() 增長API相關信息 * 經過select()函數返回一個ApiSelectorBuilder實例,用來控制哪些接口暴露給Swagger來展示, * 本例採用指定掃描的包路徑來定義指定要創建API的目錄。 * * @return
*/ @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors .basePackage("com.seecen.sc1904springboot.controller")) .paths(PathSelectors.any()) .build(); } /** * 建立該API的基本信息(這些基本信息會展示在文檔頁面中) * 訪問地址:http://項目實際地址/swagger-ui.html * @return
*/
private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot中使用Swagger2構建RESTful APIs") .description("Spring Boot中使用Swagger2構建RESTful APIs") .termsOfServiceUrl("http://www.geek5.cn") .contact(new Contact("calcyu","http://geek5.cn","hi@geek5.cn")) .version("1.0") .build(); } }
註解
@Api:用在類上,說明該類的做用。
@ApiOperation:註解來給API增長方法說明。
@ApiImplicitParams : 用在方法上包含一組參數說明。
@ApiImplicitParam:用來註解來給方法入參增長說明。
@ApiResponses:用於表示一組響應
@ApiResponse:用在@ApiResponses中,通常用於表達一個錯誤的響應信息
code:數字,例如400
message:信息,例如"請求參數沒填好"
response:拋出異常的類
@ApiModel:描述一個Model的信息(通常用在請求參數沒法使用@ApiImplicitParam註解進行描述的時候)
@ApiModelProperty:描述一個model的屬性
注意:@ApiImplicitParam的參數說明
控制層controller(上面Swagger2類中 這個
包下全部控制層的方法都會獲取)
package com.seecen.sc1904springboot.controller; import com.seecen.sc1904springboot.pojo.RESTfullResult; import com.seecen.sc1904springboot.pojo.User; import com.seecen.sc1904springboot.service.UserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import io.swagger.models.auth.In; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController // 返回的全部都是json的
@RequestMapping("/user") @Api("用戶的增刪改查功能") public class UserController2 { @Autowired private UserService userService; // emp/insert獲取用戶信息 @PathVariable("empno")Integer empno
@GetMapping("/{id}") @ApiOperation("根據id主鍵返回用戶信息") @ApiImplicitParam(name = "id",value = "用戶編號",required = true,dataType = "json") public RESTfullResult<User> getAllUsers(@PathVariable("id")Integer id) { User list = userService.selectByPrimaryKey(id); return RESTfullResult.success(list); } }
項目開始運行了 訪問測試地址:http://項目實際地址/swagger-ui.html
![](http://static.javashuo.com/static/loading.gif)
而後就能夠對控制層的全部方法進行測試了
測一個添加方法吧
![](http://static.javashuo.com/static/loading.gif)
執行後
![](http://static.javashuo.com/static/loading.gif)
執行成功了
數據庫看看
ok
這就是Swaggerui框架測試