1. 引入依赖
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
2. application配置
spring: mvc: pathmatch: matching-strategy: ant_path_matcher
3. SwaggerConfig 配置
import com.fasterxml.classmate.TypeResolver; import org.springframework.beans.factory.annotation.Autowired; 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.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.math.BigDecimal; @Configuration @EnableOpenApi public class SwaggerConfig { @Autowired private TypeResolver typeResolver; /** * 创建API应用 * apiInfo() 增加API相关信息 * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现, * 本例采用指定扫描的包路径来定义指定要建立API的目录。 * * @return */ @Bean public Docket desertsApi2() { return new Docket(DocumentationType.OAS_30) .apiInfo(apiInfo("Swagger3.0", "1.0")) .select() .apis(RequestHandlerSelectors.basePackage("com.auo.controller")) .paths(PathSelectors.any()) .build() .directModelSubstitute(BigDecimal.class, String.class) // .additionalModels(typeResolver.resolve(XXXModule.class), typeResolver.resolve(XXXModule.class), ...) //这里将'YourEntity'类加入swagger文档中 .groupName("api信息") .enable(true); } /** * 创建该API的基本信息(这些基本信息会展现在文档页面中) * 访问地址:http://ip:port/swagger-ui.html * * @return */ private ApiInfo apiInfo(String title, String version) { return new ApiInfoBuilder() .title(title) .description("api信息") .contact(new Contact("GMS", "", "")) .termsOfServiceUrl("") .version(version) .build(); } }
4.可通过application.yml中的配置,动态开关swagger
springfox: documentation: enabled: true
标签:swagger,return,springboot,documentation,3.0,import,springfox,class From: https://www.cnblogs.com/uoky/p/18613981