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

相关推荐
布列瑟农的星空15 小时前
SSE与流式传输(Streamable HTTP)
前端·后端
程序员阿鹏15 小时前
责任链模式
java·spring·servlet·tomcat·maven·责任链模式
开心就好202515 小时前
使用 HBuilder 上架 iOS 应用时常见的问题与应对方式
后端
@淡 定15 小时前
Java内存模型(JMM)详解
java·开发语言
开心猴爷16 小时前
Swift IPA 混淆在工程实践中的方式,分析仅依赖源码层混淆的局限性
后端
czhc114007566316 小时前
C# 1221
java·servlet·c#
用户40993225021216 小时前
Vue3 v-if与v-show:销毁还是隐藏,如何抉择?
前端·vue.js·后端
黄俊懿16 小时前
【深入理解SpringCloud微服务】Seata(AT模式)源码解析——全局事务的回滚
java·后端·spring·spring cloud·微服务·架构·架构师
派大鑫wink16 小时前
【Day12】String 类详解:不可变性、常用方法与字符串拼接优化
java·开发语言
Java编程爱好者16 小时前
SpringBoot启动太慢?几个优化技巧
后端