SpringDoc集成到Springboot

1.Maven引入jar包

           <dependency>
                <groupId>org.springdoc</groupId>
                <artifactId>springdoc-openapi-ui</artifactId>
                <version>1.6.14</version> 
            </dependency>

2.新建OpenApiConfig文件,代码如下

package com.elane.project.ztt.config;

import io.swagger.v3.oas.annotations.enums.SecuritySchemeIn;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
import io.swagger.v3.oas.annotations.security.SecurityScheme;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//swagger显示Authorization
//@SecurityScheme(
//        name = "bearerAuth",  // 安全方案的唯一名称
//        type = SecuritySchemeType.HTTP,
//        scheme = "bearer",
//        bearerFormat = "JWT"  // 可选,表明 Token 格式为 JWT
//)
//将未登录header中的Authorization替换为sid
@SecurityScheme(
        name = "sid",
        type = SecuritySchemeType.APIKEY,
        in = SecuritySchemeIn.HEADER,
        paramName = "sid"
)
@Configuration
public class OpenApiConfig {

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title("API文档")
                        .version("1.0")
                        .description("API文档描述"))
                .addSecurityItem(new SecurityRequirement().addList("sid"));
    }
}

3.application.yml文件增加如下配置

springdoc:
  api-docs:
    path: /v3/api-docs # 更改 API 文档路径
  swagger-ui:
    path: /swagger-ui.html # 更改 Swagger UI 路径
    disable-swagger-default-url: true # 禁用默认 URL 加载
    operations-sort-parameter: alpha # 按字母顺序排序 API

4.注意 

如果在项目中使用过SpringFox,需要将SpringFox的依赖全部移除 

### 集成 Springdoc 生成 API 文档 为了在 Spring Boot 3.4.x 中集成 springdoc 并生成 API 文档,需遵循特定的步骤和注意事项。 #### 创建 Spring Boot Web 项目 创建一个新的 Spring Boot web 应用程序,确保所使用的版本为 Spring Boot 3.4.x 和 JDK 17 或更高版本[^1]。 #### 添加 Maven 依赖项 编辑 `pom.xml` 文件并加入必要的依赖项以支持 OpenAPI UI 功能: ```xml <dependencies> <!-- Other dependencies --> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.0.4</version> </dependency> <!-- Optional: For enhanced Swagger UI features --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId> <version>4.4.0</version> </dependency> </dependencies> ``` 上述代码片段展示了如何引入 springdoc 的核心库以及可选的 knife4j 扩展来增强用户体验[^2]。 #### 启动应用程序后的访问路径 完成以上设置之后,在启动应用后可以通过浏览器访问 `/swagger-ui.html` 来查看自动生成的 RESTful 接口文档页面。对于某些新版本可能需要通过 `/v3/api-docs/swagger-ui/` 访问 swagger ui 页面。 #### 自定义配置(如果必要) 可以在项目的 application.properties 或者 yml 文件里添加一些额外属性来自定义行为,比如更改默认端点位置等: ```properties # application.yml or .properties file entries springdoc.api-docs.path=/api/v1/docs springdoc.swagger-ui.path=/api/v1/ui ``` 这样可以调整 api docs 及其对应的 ui 路径[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值