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

相关推荐
lang201509286 分钟前
深入解析Java资源加载机制
java·开发语言·python
爱笑的眼睛1140 分钟前
自动机器学习组件的深度解析:超越AutoML框架的底层架构
java·人工智能·python·ai
⑩-1 小时前
简单业务异常类
java
乘风!1 小时前
NSSM启动tomcat部署Java程序
java·服务器·后端·tomcat
BBB努力学习程序设计1 小时前
Java 21虚拟线程与平台线程:JVM层面的深度对比与实现原理
java
代码无疆1 小时前
学点java字节码更易于理解一些特殊的java语法效果
java·后端
BBB努力学习程序设计1 小时前
Java 8日期时间API完全指南:告别Date和Calendar的混乱时代
java
不能只会打代码1 小时前
力扣--3433. 统计用户被提及情况
java·算法·leetcode·力扣
知青先生1 小时前
E9项目调试方式
java·ide
本地运行没问题1 小时前
从零散编译到一键打包:Maven如何重塑Java构建流程
java