在开发中提供接口文档有些时候是一件费时费力的事情,但又不得不做,现在使用swagger完全可以轻松的梳理出文档,今天记录一下springboot集成swagger2的入门教程。
首先创建springboot的项目,导入需要的依赖
<!-- 集成swagger--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>开启对swagger2的支持
@Configuration @EnableSwagger2 //开启对swagger的支持 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .pathMapping("/") .select() .apis(RequestHandlerSelectors.basePackage("com.example.swaggerui.controller"))//要扫描的接口 .paths(PathSelectors.any()) .build().apiInfo(spiInfo()); } public ApiInfo spiInfo(){ return new ApiInfoBuilder() .title("SpringBoot整合Swagger")//标题 .description("SpringBoot整合Swagger练习demo")//描述 .version("9.0")//版本 .contact(new Contact("测试","blog.csdn.net","aaa@gmail.com"))//练习 .license("The Apache License") .licenseUrl("http://www.baidu.com") .build(); } }现在springboot集成swagger2就已经完成了,启动项目,输入http://localhost:8080/swagger-ui.html,
如果能看到如下页面,就说明已经集成成功
现在写一个接口
import com.example.swaggerui.entity.Department; import com.example.swaggerui.service.DepartmentService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; /** * (Department)表控制层 * * @author makejava * @since 2020-07-02 15:42:20 */ @RestController @RequestMapping("department") @Api(tags = "部门接口管理") public class DepartmentController { /** * 服务对象 */ @Resource private DepartmentService departmentService; /** * 通过主键查询单条数据 * * @param id 主键 * @return 单条数据 */ @GetMapping("selectOne") @ApiOperation("查询单个部门信息") public Department selectOne(Integer id) { return this.departmentService.queryById(id); } }此时启动项目,登录swagger的页面,就可以清晰的看到此接口的接口信息,本文章制作集成的入门,至于swagger的各种注解的使用和接口的详细使用方式后续会补充