springboot 2.1.6.RELEASE整合Swagger2
一、引入依赖
1 <modelVersion>4.0.0</modelVersion> 2 <groupId>com.badcat</groupId> 3 <artifactId>demo</artifactId> 4 <version>0.0.1-SNAPSHOT</version> 5 <packaging>jar</packaging> 6 7 <properties> 8 <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> 9 </properties> 10 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>2.1.6.RELEASE</version> 15 <relativePath/> 16 </parent> 17 18 <dependencies> 19 <dependency> 20 <groupId>org.springframework.boot</groupId> 21 <artifactId>spring-boot-starter-web</artifactId> 22 </dependency> 23 24 <!-- swagger2 配置 --> 25 <dependency> 26 <groupId>io.springfox</groupId> 27 <artifactId>springfox-swagger2</artifactId> 28 <version>2.4.0</version> 29 </dependency> 30 <dependency> 31 <groupId>io.springfox</groupId> 32 <artifactId>springfox-swagger-ui</artifactId> 33 <version>2.4.0</version> 34 </dependency> 35 <dependency> 36 <groupId>com.github.xiaoymin</groupId> 37 <artifactId>swagger-bootstrap-ui</artifactId> 38 <version>1.6</version> 39 </dependency> 40 </dependencies>
二、创建配置类
1 package com.badcat.config; 2 3 import org.springframework.context.annotation.Bean; 4 import org.springframework.context.annotation.Configuration; 5 import springfox.documentation.builders.ApiInfoBuilder; 6 import springfox.documentation.builders.PathSelectors; 7 import springfox.documentation.builders.RequestHandlerSelectors; 8 import springfox.documentation.service.ApiInfo; 9 import springfox.documentation.service.Contact; 10 import springfox.documentation.spi.DocumentationType; 11 import springfox.documentation.spring.web.plugins.Docket; 12 import springfox.documentation.swagger2.annotations.EnableSwagger2; 13 14 @Configuration 15 @EnableSwagger2 16 public class Swagger2 { 17 18 // http://localhost:8080/swagger-ui.html 19 // http://localhost:8080/doc.html 20 21 // 配置swagger2核心配置 docket 22 @Bean 23 public Docket createRestApi() { 24 return new Docket(DocumentationType.SWAGGER_2) // 指定api类型为swagger2 25 .apiInfo(apiInfo()) // 用于定义api文档汇总信息 26 .select() 27 .apis(RequestHandlerSelectors 28 .basePackage("com.badcat.controller")) // 指定controller包 29 .paths(PathSelectors.any()) // 所有controller 30 .build(); 31 } 32 33 private ApiInfo apiInfo() { 34 return new ApiInfoBuilder() 35 .title("后台接口api") // 文档页标题 36 .contact(new Contact("badcat", 37 "https://www.xxxx.com", 38 "xxxx@xxxx.com")) // 联系人信息 39 .description("后台api文档") // 详细信息 40 .version("1.0.1") // 文档版本号 41 .termsOfServiceUrl("https://www.xxxx.com") // 网站地址 42 .build(); 43 } 44 45 }
三、启动项目
输入网址,可以显示出所有接口信息,可以直接调试。下边是两种风格的
http://localhost:8080/swagger-ui.html, http://localhost:8080/doc.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· 【全网最全教程】使用最强DeepSeekR1+联网的火山引擎,没有生成长度限制,DeepSeek本体