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

相关推荐
廋到被风吹走11 分钟前
安全防护深度解析:敏感信息加密、密码哈希与密钥管理实战
java
biyezuopinvip32 分钟前
基于Spring Boot的投资理财系统设计与实现(毕业论文)
java·spring boot·vue·毕业设计·论文·毕业论文·投资理财系统设计与实现
iAkuya39 分钟前
(leetcode)力扣100 75前K个高频元素(堆)
java·算法·leetcode
极客先躯42 分钟前
高级java每日一道面试题-2025年7月17日-基础篇[LangChain4j]-如何实现模型的负载均衡和故障转移?
java·langchain·负载均衡·重试机制·负载均衡实现·故障转移实现·多级降级
用户34140819912544 分钟前
Request(下发) 和 Indication(上报) 的完整流程
后端
何中应1 小时前
使用jvisualvm提示“内存不足”
java·jvm·后端
何中应1 小时前
如何手动生成一个JVM内存溢出文件
java·jvm·后端
小灵吖1 小时前
LangChain4j Tool(Function Call)
java·后端
Lxinccode1 小时前
AI编程(3) / claude code[3] : 更新apiKey
java·数据库·ai编程·claude code