1.引入maven依赖,本文采用2.9.2版本
maven仓库地址:https://mvnrepository.com(根据情况选择自己的版本)
<!-- swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
2.编写Swagger2Config配置文件,加入@Configuration和@EnableSwagger2注解
@Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() // 路径根据自己项目配置 .apis(RequestHandlerSelectors.basePackage("com.ohes.missclass.business.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("因病缺课后台api文档") .description("简单优雅的restful风格") .version("1.0") .build(); } }
3.编写TestController
@RestController @RequestMapping("/business/school") @Api(tags = "学校管理相关接口") public class TestController { @ApiOperation(value = "接口的功能介绍",notes = "提示接口使用者注意事项",httpMethod = "GET") @ApiImplicitParam(dataType = "string",name = "name",value = "姓名",required = true) @GetMapping("/hello") public String hello(String name) { return "hello "+ name; } }
4.若项目配置了shiro,则需要配置shiro免认证,不然会出现如下图情况
filterMap.put("/swagger-ui.html","anon"); filterMap.put("/swagger-resources","anon"); filterMap.put("/swagger-resources/configuration/security","anon"); filterMap.put("/swagger-resources/configuration/ui","anon"); filterMap.put("/v2/api-docs","anon"); filterMap.put("/webjars/springfox-swagger-ui/**","anon");
5.测试SpringBoot集成swagger2是否成功,访问http://172.1.5.133:9528/swagger-ui.html
server: port: 9528 address: 172.1.5.133
出现以下页面说明访问成功!!!!!!!!!!!!