springboot 接入 swagger 3.0
1. 引入依赖
1 2 3 4 5 | <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version> 3.0 . 0 </version> </dependency> |
2. application配置
1 2 3 4 | spring: mvc: pathmatch: matching-strategy: ant_path_matcher |
3. SwaggerConfig 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | 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
1 2 3 | springfox: documentation: enabled: true |
标签:
swagger
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析