最完整版-Springboot3集成Knife4j
Spring Boot 3 整合 Knife4j 的完整版代码示例如下:
- 在pom.xml中添加Knife4j的依赖:
<dependencies>
<!-- 其他依赖 -->
<!-- Knife4j依赖 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
- 在application.properties或application.yml中配置Knife4j:
# application.properties
# 可以根据需要配置Knife4j相关的属性,例如界面的标题、版本描述等
- 在Spring Boot启动类上添加
@EnableKnife4j
注解启用Knife4j:
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableKnife4j // 启用Knife4j
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
// 其他配置或组件
}
- 创建一个API文档的控制器:
import com.github.xiaoymin.knife4j.core.OpenApiExtInjector;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;
import java.util.List;
@RestController
@RequestMapping("/knife4j")
@Api(tags = "Knife4j测试接口")
public class Knife4jController {
@GetMapping("/ui")
@ApiOperation("Knife4j的UI接口")
public UiConfiguration uiConfiguration() {
return UiConfigurationBuilder.builder()
.build();
}
@GetMapping("/openapi")
@
评论已关闭