SpringBoot基于Swagger2構建API文檔過程解析
更新時間:2019年11月06日 08:33:48 作者:我好難啊upup
這篇文章主要介紹了SpringBoot基于Swagger2構建API文檔過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
一、添加依賴
<!--SpringBoot使用Swagger2構建API文檔的依賴--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
二、創(chuàng)建Swagger2配置類
package com.offcn.config; 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.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration//表示該類為一個配置類,相當于spring中的xml配置文件 @EnableSwagger2 //開啟在線文檔 public class SwaggerConfig { //1.聲明 api 文檔的屬性 private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot中使用Swagger2構建RESTful APIs") .description("優(yōu)就業(yè)") .termsOfServiceUrl("http://www.ujiuye.com/") .contact("小劉同學") .version("1.0") .build(); } //配置核心配置信息 public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.offcn.controller")) .paths(PathSelectors.any()) .build(); } }
三、修改Controller 增加文檔注釋
通過@ApiOperation注解來給API增加說明
通過@ApiImplicitParams@ApiImplicitParam注解來給參數(shù)增加說明
package com.offcn.controller; import com.offcn.dao.UserDao; import com.offcn.entity.User; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RequestMapping("/rest") @RestController public class RestFulController { @Autowired private UserDao userDao; @GetMapping("/getUserById") @ApiOperation(value="查找指定id用戶信息", notes="根據(jù)id查找用戶信息") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Integer"), }) public User getUserById(Integer id){ User user = userDao.getOne(id); return user; } @DeleteMapping("/del") @ApiOperation(value="刪除指定id用戶信息", notes="根據(jù)id刪除用戶信息") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Integer"), }) public String delUserById(Integer id){ userDao.deleteById(id); return "success"; } }
四、查看Swagger2文檔
重啟項目
訪問:
http://localhost:8080/swagger-ui.html
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
mybatis-plus IdWorker生成的Id和返回給前臺的不一致的解決
這篇文章主要介紹了mybatis-plus IdWorker生成的Id和返回給前臺的不一致的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03Idea中如何調出Run dashboard 或services窗口
這篇文章主要介紹了Idea中如何調出Run dashboard 或services窗口問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03SpringBoot使用攔截器Interceptor實現(xiàn)統(tǒng)一角色權限校驗
角色權限校驗,是保證接口安全必備的能力:有權限才可以操作,所以,一般對于這種通用邏輯,推薦不與主業(yè)務邏輯耦合,那么怎么來解耦,那么本文小編就給大家詳細講解如何使用攔截器Interceptor實現(xiàn)統(tǒng)一角色權限校驗,需要的朋友可以參考下2023-07-07