Springboot整合Swagger2配置

1、导入依赖

copy
<!-- Swagger API文档 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> <exclusions> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> </exclusion> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.6</version> </dependency> <!-- # 增加两个配置解决 NumberFormatException --> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.5.22</version> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> <version>1.5.22</version> </dependency>

2、Swagger2Config.java的配置

copy
@Slf4j @Configuration @EnableWebMvc @EnableSwagger2 @ComponentScan(basePackages = "com.ma.controller") public class Swagger2Config implements WebMvcConfigurer { @Bean public Docket controller_api(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo())//接口文档信息 .select() //查找 .apis(RequestHandlerSelectors.basePackage("com.ma.controller")) //生成文档的包 .paths(PathSelectors.any()) //路径 .build() //构建 .groupName("控制类接口"); //组名 } @Bean public Docket domain_api(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.ma.domain")) .paths(PathSelectors.any()) .build() .groupName("实体类"); } @Bean public Docket untiy_api(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.ma.untiy")) .paths(PathSelectors.any()) .build() .groupName("工具类"); } public ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("第一个接口文档")//接口文档标题 .version("v-1")//版本 .description("这是第一个接口文档测试")//接口文档描述 .license("The Apache License, Version 2.0")//执照 .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html") //执照地址 .contact(new Contact("张三","https://www.baidu.com","17855454@qq.com")) //团队 .build(); //构建接口文档信息 } /** * * 显示swagger-ui.html文档展示页,还必须注入swagger资源: * * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } }

3、在需要生成文档的类上加上注解

copy
@Api(tags = "控制类") //在接口上添加注解 @ApiOperation(value = "返回值方法")//在方法上添加注解 @ApiModelProperty(value = "id") //实体类字段上的注解
posted @   花椒蛋炒饭  阅读(96)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起