restFull api接口


RestFull api接口

  前后端分離開發的接口規范

  什么是RestFull 是目錄比較流行的api設計規范

  注:restfull api規范應用場景,前后端分離的項目中

數據接口的現場 例如:

  /users/999 獲取ID為999的信息

  /users/list  獲取所有的用戶信息

  /users/add  打開添加的頁面

  /users/save 新增數據

  /users/edit 打開修改的頁面

  /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框架測試

  1. 導入依賴包
  <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

然后就可以對控制層的所有方法進行測試了
   
測一個添加方法吧
    

 執行后

執行成功了

數據庫看看

ok

這就是Swaggerui框架測試


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM