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

相关推荐
用户8356290780515 小时前
无需 Office:Python 批量转换 PPT 为图片
后端·python
啊哈灵机一动5 小时前
使用golang搭建一个nes 模拟器
后端
日月云棠6 小时前
各版本JDK对比:JDK 25 特性详解
java
间彧6 小时前
SpringBoot + ShardingSphere 读写分离实战指南
后端
砍材农夫7 小时前
订单超时
后端
树獭叔叔7 小时前
06-大模型如何"学习":从梯度下降到AdamW优化器
后端·aigc·openai
用户8307196840827 小时前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
得鹿7 小时前
MySQL基础架构与存储引擎、索引、事务、锁、日志
后端
程序员飞哥7 小时前
Block科技公司裁员四千人,竟然是因为 AI ?
人工智能·后端·程序员
JavaGuide8 小时前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code