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

相关推荐
雪碧聊技术几秒前
如何界定人工智能和java开发二者的关系?
java·人工智能·二者关系界定
Chase_______3 分钟前
【JAVA基础指南(四)】快速掌握类和对象
java·开发语言
喝茶与编码16 分钟前
上线不背锅:三种主流发布策略原理、优劣与实战建议
后端
muxin-始终如一24 分钟前
Maven HTTP 仓库被阻止问题解决总结
java·http·maven
武斌28 分钟前
需要独立的作业队列?看看Quartz增强框架Quartz Plus
java·spring boot·后端
while(1){yan}34 分钟前
Mybatis动态SQL
数据库·spring boot·sql·spring·java-ee·mybatis
子洋42 分钟前
AI Agent 设计模式 - PlanAndExecute 模式
前端·人工智能·后端
廋到被风吹走1 小时前
【Java】【JVM】即时编译解析:C1/C2、分层编译、OSR与日志分析
java·开发语言·jvm
计算机毕设指导61 小时前
基于微信小程序的扶贫助农系统【源码文末联系】
java·spring boot·后端·微信小程序·小程序·tomcat·maven
Overt0p1 小时前
抽奖系统(5)
java·redis·springboot