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

相关推荐
非优秀程序员3 小时前
第07章 孙德良的阳谋|《互联网产品恩仇录》
javascript·后端
非优秀程序员3 小时前
第06章 浙江帮的内战|《互联网产品恩仇录》
后端
非优秀程序员3 小时前
第08章 马云的反击|《互联网产品恩仇录》
后端
非优秀程序员3 小时前
题记|《互联网产品恩仇录》
后端·程序员·创业
非优秀程序员3 小时前
第04章 邮箱大战,163 与 QQ 的暗战|《互联网产品恩仇录》
java·vue.js·架构
知已难寻4 小时前
Docker 入门:容器技术与 Docker 基础
java·运维·服务器·docker·云原生·容器
文丑颜不良啊6 小时前
LeetCode2481~2500题目解析
java·leetcode
Flynn Liang7 小时前
同一需求三套写法:用 Spring AI、LangChain4j、AgentScope Java 各实现一个带工具调用的 RAG 助手(实战对比)
java·人工智能·大模型·架构设计·spring ai·langchain4j·agentscope
Gopher_HBo7 小时前
Go CPU 剖析的"地基":信号、CAS、写屏障、无锁环形缓冲
后端
panbook7 小时前
从0到1实现一个分布式时序数据库(二):写入成功之后,如何用 WAL 找回数据
后端