Springboot整合Knife4j接口文档
Spring Boot整合Knife4j创建Swagger接口文档的步骤如下:
- 在
pom.xml
中添加Knife4j的依赖:
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>最新版本号</version>
</dependency>
- 在
application.yml
或application.properties
中配置Knife4j相关的属性(可选):
# 配置Knife4j的相关属性,例如标题、描述等
knife4j:
basic:
enable: true
doc-title: "接口文档标题"
doc-description: "接口文档描述"
- 在Spring Boot启动类上添加
@EnableKnife4j
注解启用Knife4j:
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableKnife4j // 启用Knife4j
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 使用Swagger注解标注Controller和方法:
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;
@RestController
@RequestMapping("/api/demo")
@Api(tags = "示例接口")
public class DemoController {
@GetMapping("/greet")
@ApiOperation("问候接口")
public String greet() {
return "Hello, Knife4j!";
}
}
完成以上步骤后,重启Spring Boot应用,访问http://<host>:<port>/doc.html
即可看到Knife4j生成的Swagger接口文档。
评论已关闭