SpringBoot v2.6.13 整合 swagger

在Spring Boot 2.6.13中整合Swagger需要以下步骤:

添加Swagger依赖到pom.xml:

xml 复制代码
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

创建Swagger配置类:

java 复制代码
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

在application.properties或application.yml中配置Swagger:

application.properties

复制代码
springfox.documentation.enabled=true

或者

application.yml

复制代码
springfox:
  documentation:
    enabled: true

启动Spring Boot应用,并访问 http://:/swagger-ui.html 查看Swagger文档。

确保Spring Boot版本与Swagger版本兼容。如果使用的Spring Boot版本是2.6.13,那么可以使用与Spring Boot 2.6兼容的Swagger版本,例如2.9.2。如果Spring Boot升级到了新版本,可能需要更新Swagger依赖到对应的新版本。

相关推荐
别来无恙1492 小时前
Spring Boot + ECharts 极简整合指南:从零实现动态数据可视化大屏
spring boot·信息可视化·echarts
刘火锅3 小时前
Bug 记录:SecureRandom.getInstanceStrong()导致验证码获取阻塞
spring boot·spring·spring cloud·bug
hrrrrb3 小时前
【Spring Boot 快速入门】八、登录认证(一)基础登录与认证校验
spring boot·后端
运维帮手大橙子4 小时前
完整的登陆学生管理系统(配置数据库)
java·前端·数据库·eclipse·intellij-idea
王大锤·4 小时前
基于spring boot的个人博客系统
java·spring boot·后端
bobz9654 小时前
QT designer 常用技巧
后端
shi57834 小时前
C# 常用的线程同步方式
开发语言·后端·c#
sg_knight5 小时前
Spring Cloud Gateway全栈实践:动态路由能力与WebFlux深度整合
java·spring boot·网关·spring·spring cloud·微服务·gateway
没逻辑5 小时前
抗量子密码技术(PQC)演变
后端·量子计算
JosieBook5 小时前
【IDEA】IntelliJ IDEA 中文官方文档全面介绍与总结
java·ide·intellij-idea