按以下步骤集成即可。
pom
文件集成:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.0.2</version>
</dependency>
注意版本,可以在终端输入
mvn dependency:tree
检查是否有依赖冲突
- 配置类:
package com.coder.community.config;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SwaggerConfig {
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.info(new Info()
.title("coder论坛接口文档")
.version("1.0.0")
.description("正在开发中"));
}
}
- 配置文件
# Swagger UI 路径
springdoc.api-docs.path=/v3/api-docs
springdoc.swagger-ui.path=/swagger-ui.html
-
访问Swagger UI
启动Spring Boot应用后,访问
http://localhost:8080/上下文路径/swagger-ui/
来查看生成的API文档。比如我的上下文路径在配置文件中这样写:
server.servlet.context-path=/community
那我查看接口文档应该访问[Swagger UI](http://localhost:8080/community/swagger-ui/index.html)