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依赖到对应的新版本。

相关推荐
麻瓜code1 小时前
【LeetCode】两数相加:模拟加法竖式,用进位 flag 一次遍历解决
java
zzzll11112 小时前
Spring Boot 实现数据脱敏:自定义注解 + Jackson 序列化器
java·spring boot·后端
vx-Biye_Design6 小时前
SSM伴侣动物伴护星小程序06330-计算机课程设计、毕业设计
spring boot·后端·elasticsearch·小程序·架构·课程设计·idea
2分钟速写快排8 小时前
什么是 RAG?如何用 RAG 实现一个用户记忆?
前端·后端·ai编程
面向Google编程8 小时前
Apache Iceberg Variant 类型:v3 半结构化数据不再「二选一」
大数据·后端
lhldsg9 小时前
全民健身解决方案小程序开发:从0到1的技术实战
java·数据库·需求分析
jason成都9 小时前
Spring WebFlux 适配达梦新方案|dm‑r2dbc:Netty 异步传输的实验性 R2DBC 驱动
java·后端·spring
泡海椒9 小时前
内置SPI函数库详解:JQuick-Java Builtin工具类实战用法
java·开发语言·python
辰烨chenye9 小时前
LeetCode Hot 100 题解 · 二分篇
java·算法·leetcode
小羊没烦恼!10 小时前
Hello Web API系列教程——Web API与国际化
java·服务器·前端·javascript·php