1. 引入依赖
在 Swagger3 中不需要引入依赖springfox-swagger2、springfox-swagger-ui,只需要引入依赖 springfox-boot-starter 即可,因为 springfox-boot-starter 依赖已将 springfox-swagger2、springfox-swagger-ui 引入,通过下面代码引入 Swagger3 依赖:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
2. 自定义配置swagger
package com.qing.config; @Configuration @EnableOpenApi public class SwaggerConfig { }
上面配置完成后,运行时会有如果报错:
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
Springboot2.6 中将 SpringMVC 默认路径匹配策略从 AntPathMatcher 更改为 PathPatternParser,所以需要在 application.yml 文件中添加如下配置即可:
spring: mvc: pathmatch: matching-strategy: ant_path_matcher
Swagger3 完整配置
package com.qing.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.oas.annotations.EnableOpenApi; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import java.util.ArrayList; @Configuration //配置类 @EnableOpenApi public class SwaggerConfig { @Bean public Docket getDocket() { Docket docket = new Docket(DocumentationType.OAS_30); docket.apiInfo(getApiInfo()); // RequestHandlerSelectors.basePackage 指定包下文件开启 swagger // RequestHandlerSelectors.any() 所以包下文件开启 swagger // RequestHandlerSelectors.none() 所有包下文件都不开启 swagger // RequestHandlerSelectors.withClassAnnotation() 扫描有指定注解的类 docket.select().apis(RequestHandlerSelectors.basePackage("com.qing.controller.swagger")).build(); // PathSelectors.ant() 扫描指定目录下的文件 // PathSelectors.any() 扫描所有文件 // PathSelectors.none() 所有文件都不扫描 // PathSelectors.regex() 按正则方式扫描文件 docket.select().paths(PathSelectors.ant("/qing/**")).build(); return docket; } protected ApiInfo getApiInfo(){ Contact contact = new Contact("龙卷风", "https://www.sviping.com", "sunwq@sviping.com"); return new ApiInfo("Api 文档", "我的Api文档", "1.0", "urn:tos", contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList()); } @Bean public Docket getDocket1() { Docket docket = new Docket(DocumentationType.OAS_30); docket.apiInfo(getApiInfo()); docket.groupName("docket1"); docket.select().apis(RequestHandlerSelectors.basePackage("com.qing.controller.swagger")).build(); return docket; } @Bean public Docket getDocket2() { Docket docket = new Docket(DocumentationType.OAS_30); docket.apiInfo(getApiInfo()); docket.groupName("docket2"); docket.select().apis(RequestHandlerSelectors.basePackage("com.qing.controller.swagger")).build(); return docket; } @Bean public Docket getDocket3() { Docket docket = new Docket(DocumentationType.OAS_30); docket.apiInfo(getApiInfo()); docket.groupName("docket3"); docket.select().apis(RequestHandlerSelectors.basePackage("com.qing.controller.swagger")).build(); return docket; } }
3. 控制器代码
package com.qing.controller.swagger; import io.swagger.annotations.*; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/swagger") @Tag(name = "接口文档title", description = "接口文档description") public class SwaggerController { @GetMapping(value = "/user") @Operation(summary ="接口方法描述") @ApiResponses({ @ApiResponse(code = 403, message = "无权限"), @ApiResponse(code = 404, message = "接口未找到") }) public User user(@Parameter(name = "name", description = "用户名") String name, @Parameter(name = "age", description = "年龄") Integer age) { User user = new User(); user.setName(name); user.setAge(age); System.out.println(user); return user; } } @ApiModel("user类") class User{ @Schema(name = "姓名", title = "name 的title") private String name; private int age; public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public int getAge() { return age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
4. 常用注解说明
注解 | 位置 | 说明 |
@EnableOpenApi | swagger配置类 | |
@Tag | Controller | 类的说明信息 |
@Operation | Controller方法 | 类的方法说明 |
@Parameter | Controller方法参数 | 方法参数说明 |
@ApiResponses | Controller方法 | 一组响应 |
@ApiResponse | @ApiResponses | 一条错误响应信息 |
@ApiModel | 响应类 | 返回的响应信息 |