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

相关推荐
m0_748238637 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
Forget the Dream28 分钟前
设计模式之责任链模式
java·c++·设计模式·责任链模式
jonyleek31 分钟前
「JVS更新日志」低代码、企业会议、智能BI、智能排产2.26更新说明
java·大数据·低代码·数据分析·软件需求
计算机小白一个1 小时前
蓝桥杯 Java B 组之最短路径算法(Dijkstra、Floyd-Warshall)
java·数据结构·算法·蓝桥杯
曼岛_1 小时前
[密码学实战]Java实现SM4加解密(ecb,cbc)及工具验证
java·密码学
菜鸟阿达1 小时前
spring boot 2.7 + seata +微服务 降级失败问题修复
spring boot·后端·微服务
清河__1 小时前
【Go】十七、grpc 服务的具体功能编写
开发语言·后端·golang
Forget the Dream1 小时前
设计模式之代理模式
java·c++·设计模式·代理模式
张声录11 小时前
国密算法Sm2工具类--golang实现版
开发语言·后端·golang
帅的飞起来1 小时前
设计模式--spring中用到的设计模式
java·spring·设计模式