Spring boot+swagger2构建restful api文档

这篇文章,我们简单介绍一下 boot整合构建 api文档 。
什么是
我们在开发一些风格的api时,通常会为调用者提供一套 api说明文档 。当我们的接口有调整时,就需要去更新api文档 。而是这样的一个框架,可以在开发接口的过程中通过注解的方式来描述我们开发的接口,从而保证 api文档和接口代码的实时同步性 。
boot整合
1引入依赖
在pom.xml文件中引入依赖

Spring boot+swagger2构建restful api文档

文章插图
io.springfoxspringfox-swagger22.2.2io.springfoxspringfox-swagger-ui2.2.2
2编写配置类
.java
package com.yuxiaolong;import org.springframework.beans.factory.annotation.Value;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.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;/*** @author yuxl* swagger配置类*/@Configuration@EnableSwagger2public class Swagger2 {@Value("${swagger.show}")private boolean swaggerShow;@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).enable(swaggerShow).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.yuxiaolong.controller")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("MY Site Swagger Restful API").description("主要罗列了应用提供的api接口").termsOfServiceUrl("https://www.wowjia.top/wojia/").contact("yuxioalong").version("1.0").build();}}
3在工程中的 api中通过注解描述api
package com.yuxiaolong.controller;import com.yuxiaolong.model.ModuleDO;import com.yuxiaolong.service.ModuleService;import com.yuxiaolong.utils.ApiResponse;import io.swagger.annotations.ApiOperation;import io.swagger.annotations.ApiParam;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;/*** @author yuxl*/@RestController@RequestMapping("/module")public class ModuleController {@AutowiredModuleService moduleService;@ApiOperation("新增模块")@PostMapping("/add")@ResponseBodypublic ApiResponse addModule(@RequestBody@ApiParam(name = "moduleDO", value = "http://www.kingceram.com/post/模块实体类", required = true)ModuleDO moduleDO){moduleService.addModule(moduleDO);returnApiResponse.success();}@ApiOperation("删除模块")@DeleteMapping("/delete/{id}")@ResponseBodypublic ApiResponse deleteModule(@ApiParam(name = "id", value = "http://www.kingceram.com/post/模块id", required = true)@PathVariableInteger id){moduleService.deleteModule(id);return ApiResponse.success();}@ApiOperation("更新模块")@PutMapping("/update")@ResponseBodypublic ApiResponse updateModule(@RequestBody@ApiParam(name = "moduleDO", value = "http://www.kingceram.com/post/模块实体类", required = true)ModuleDO moduleDO){moduleService.updateModule(moduleDO);return ApiResponse.success();}@ApiOperation("查询所有模块")@GetMapping("/list")@ResponseBodypublic List getModuleListByCondition(@ApiParam(name = "moduleName", value = "http://www.kingceram.com/post/模块名称", required = false)String moduleName){return moduleService.getModuleListByCondition(null);}}
Spring boot+swagger2构建restful api文档

文章插图
4启动 boot应用,访问 api
启动应用,访问:8080/-ui.html
通过该ui页面,我们还可以调用接口进行调试