Springdoc OpenAPI 的使用

概念介绍

主要是用来统一管理api接口,而不是一个一个输入url去postman或者浏览器上进行测试

  • Swagger 是一个广泛的 API 文档规范和工具集,核心是 OpenAPI 规范
  • Springfox Swagger 是 Swagger 的一个实现,专为 Spring 项目生成 Swagger 2 规范的 API 文档。
  • Springdoc OpenAPI 是 Spring 的现代化库,专注于生成符合 OpenAPI 3.0 规范的 API 文档,逐渐取代了 Springfox。

为什么要使用 Springdoc OpenAPI?

Springfox 很久每更新了,不支持spring boot 3.x , 而Springdoc OpenAPI兼容spring 3.2.2版本的(自己测试过,3.4.x不行)

前置配置工作

1> pom.xml来引入相关依赖

注意Spring Boot 版本不能太高我此时的版本是3.2.2

springdoc依赖

        <!-- 加入 springdoc 依赖 -->
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.5.0</version>
        </dependency>

<repositories>
    <!--阿里云镜像-->
    <repository>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

2> 配置yml

server:
  port: 8080 (你项目的端口号)

springdoc:
  api-docs:
    enabled: true # 开启OpenApi接口
    path: /v3/api-docs  # 自定义路径,默认为 "/v3/api-docs"
  swagger-ui:
    enabled: true # 开启swagger界面,依赖OpenApi,需要OpenApi同时开启
    path: /swagger-ui/index.html # 自定义路径,默认为"/swagger-ui/index.html"

3> 配置swagger


在config 包下编写SwaggerConfig

package org.xiaobai.forum.config;


import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author HHHY
 * @ClassName
 * @Date: 2024/4/2 14:21
 * @Description: Swagger 配置
 */

@Configuration
public class SwaggerConfig {
    @Bean
    public OpenAPI springShopOpenAPI() {
        return new OpenAPI()
                .info(new Info().title("Spring Boot 中使用 Swagger UI 构建 RESTful API")
                        .contact(new Contact())
                        .description("百草中医药信息管理平台提供的 RESTful API")
                        .version("v1.0.0")
                        .license(new License().name("Apache 2.0").url("http://springdoc.org")))
                .externalDocs(new ExternalDocumentation()
                        .description("外部文档")
                        .url("https://springshop.wiki.github.org/docs"));
    }
}

4> 访问url

 http://localhost:8080/swagger-ui/swagger-ui/index.html#/ 

就有以下成功页面显示

如果觉得不行可以看看这个佬的博客:Spring Boot 3.x 引入springdoc-openapi (内置Swagger UI、webmvc-api)_springdoc-openapi-starter-webmvc-ui-优快云博客

4> 在这上面直接去测试我们的接口

各种注解的使用

1. @Tag

@Tag 主要是用来描述Controller类的基本信息

示例代码

import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Post", description = "帖子操作相关接口")
@RestController
public class PostController {

    @GetMapping("/posts")
    public List<Post> getAllPosts() {
        return postService.getAllPosts();
    }
}

2. @Schema

@Schema描述model类里面的类和字段信息

示例代码

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "帖子模型")
public class Post {

    @Schema(description = "帖子的唯一ID", example = "1")
    private Long id;

    @Schema(description = "帖子的标题", required = true)
    private String title;

    @Schema(description = "帖子的内容")
    private String content;
}

3. @Operation 

@Operation 描述一个API操作(方法信息)

示例代码:

import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PostController {

    @Operation(summary = "获取所有帖子", description = "返回系统中所有的帖子数据")
    @GetMapping("/posts")
    public List<Post> getAllPosts() {
        return postService.getAllPosts();
    }
}

4. @Parameter

@Parameter 描述一个方法里面的参数信息

示例代码:

import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PostController {

    @GetMapping("/posts")
    public List<Post> getPosts(
            @Parameter(description = "帖子类别") @RequestParam String category,
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") int page) {
        return postService.getPostsByCategory(category, page);
    }
}

小结

Springdoc OpenAPI 对应注解描述
@Tag描述类或方法的标签,帮助分组和分类API
@Schema描述数据模型类的基本信息,标注类的描述、示例等
@Schema (字段级别)描述字段的详细信息,如描述、示例、是否必填等
@Operation描述API方法的功能,如摘要、详细描述、响应类型等
@Parameter描述请求参数的信息,如名称、描述、默认值、示例等

 把 swagger里面的接口都导入到Postman

1> 启动项目,访问地址

Swagger UI

2> 确认连接有效并且把点进去的url复制

 3> 打开Postman进入工作空间

4> 在APIs中导⼊API,并完成测试

然后就可以看见生成了一堆的接口测试

其他注意事项

1> 我们的Spring Boot 版本不要太高, 此时我使用的是Spring Boot 3.2.2

2> 我们的Postman使用的是9.31.27(8.xx不能设置Link)

### Springdoc OpenAPI 使用指南 Springdoc OpenAPI 是一种用于生成 RESTful Web 服务 API 文档的强大工具,它基于 OpenAPI 3 规范。以下是关于如何使用和配置 Springdoc OpenAPI 的详细说明。 #### 自动化功能 默认情况下,Springdoc OpenAPI 能够自动扫描项目中的控制器并生成对应的 API 文档[^1]。这意味着开发者无需手动编写大量代码来描述接口细节。 #### 基础配置 为了进一步定制 API 文档的行为或者路径,可以通过修改 `application.properties` 文件实现特定需求。例如: ```properties springdoc.api-docs.path=/api-docs springdoc.swagger-ui.path=/swagger-ui.html ``` 上述配置分别指定了 API 文档的访问地址以及 Swagger UI 页面的位置。 #### Maven 和 Gradle 集成 对于采用 Gradle 构建系统的项目来说,可以参考官方文档完成插件安装与基本集成操作[^2]。确保依赖项正确引入之后再继续其他高级选项调整工作流程。 #### 迁移至 OpenAPI 3 标准 随着技术发展,在较新的 Spring Boot 版本 (如 3.x) 中已经停止对旧版 Swagger 2 的支持。因此当面临升级场景时,则需考虑切换到符合最新标准的技术栈——即这里提到的 SpringDoc OpenAPI 加上增强显示效果用到的第三方库 Knife4j 组件组合方案[^3]。 #### 示例代码片段 下面给出一段简单的 Java 控制器样例配合必要的注解以便于理解整个过程: ```java import io.swagger.v3.oas.annotations.Operation; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/example") public class ExampleController { @Operation(summary = "获取示例数据", description = "这是一个测试方法用来展示 springdoc 功能.") @GetMapping("/data") public String getData() { return "Example Data"; } } ``` 此例子展示了如何利用 `@Operation` 注解为某个 GET 请求提供简洁明了的文字解释给最终用户查看。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值