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

相关推荐
微尘810 分钟前
C语言存储类型 auto,register,static,extern
服务器·c语言·开发语言·c++·后端
VaporGas13 分钟前
idea集成和使用Git指南
java·git·intellij-idea
计算机学姐17 分钟前
基于PHP的电脑线上销售系统
开发语言·vscode·后端·mysql·编辑器·php·phpstorm
阿乾之铭34 分钟前
spring MVC 拦截器
java·spring·mvc
码爸37 分钟前
flink 批量写clickhouse
java·clickhouse·flink
djgxfc40 分钟前
简单了解Maven与安装
java·maven
中文很快乐43 分钟前
springboot结合p6spy进行SQL监控
java·数据库·sql
丶白泽43 分钟前
重修设计模式-概览
java·设计模式
小电玩44 分钟前
谈谈你对Spring的理解
java·数据库·spring
五味香1 小时前
C++学习,动态内存
java·c语言·开发语言·jvm·c++·学习·算法