springboot3集成knife4j
时间: 2023-09-26 18:12:28 浏览: 80
你可以使用以下步骤将 Spring Boot 3 集成 Knife4j:
1. 添加 Maven 依赖:在 `pom.xml` 文件中添加以下依赖:
```xml
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
```
2. 配置 Knife4j:创建一个配置类,例如 `SwaggerConfig.java`,并添加以下内容:
```java
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API 文档")
.description("API 接口文档")
.version("1.0")
.build();
}
}
```
确保将 `com.example.controller` 替换为您自己的控制器包路径。
3. 启动应用程序:在 Spring Boot 应用程序的入口类(通常是带有 `@SpringBootApplication` 注解的类)中添加 `@EnableSwagger2` 注解。
现在,你的 Spring Boot 3 应用程序已经集成了 Knife4j,并可以通过访问 `http://localhost:port/doc.html` 来查看 API 文档。请将 `port` 替换为你的应用程序的端口号。
希望这能帮到你!如果还有其他问题,请随时问我。