Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务的接口文档。Swagger简单说就是可以帮助生成接口说明文档,操作比较简单添加注解说明,可以自动生成格式化的文档。
项目环境
- jdk17
- SpringBoot 3.4.0
- Springdoc 2.7.0
引入项目依赖
其他项目依赖根据需求引入
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>3.4.0</version><type>pom</type><scope>import</scope>
</dependency>
<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.7.0</version>
</dependency>
代码编写
输入文档地址:http://localhost:8080/swagger-ui/index.html#/ 查看具体接口文档信息的生成
注意
需要保证springdoc版本为2.7.0以上如果是以下版本则会报错:Handler dispatch failed: java.lang.NoSuchMethodError:
错误如下:
2024-12-27 14:52:57,246 [ERROR] [,,,TID: N/A,] [http-nio-8080-exec-8] [org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet]] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed: java.lang.NoSuchMethodError: 'void org.springframework.web.method.ControllerAdviceBean.<init>(java.lang.Object)'] with root cause
java.lang.NoSuchMethodError: 'void org.springframework.web.method.ControllerAdviceBean.<init>(java.lang.Object)'at org.springdoc.core.service.GenericResponseService.lambda$getGenericMapResponse$8(GenericResponseService.java:706)at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:178)at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:992)at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:575)at java.base/java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:260)at java.base/java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:616)at java.base/java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:622)at java.base/java.util.stream.ReferencePipeline.toList(ReferencePipeline.java:627)at org.springdoc.core.service.GenericResponseService.getGenericMapResponse(GenericResponseService.java:708)