整合Spring Boot 框架集成Knife4j
整合Spring Boot 和 Knife4j的步骤如下:
- 在
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
knife4j.basic.kt=your-key
knife4j.basic.md=your-secret
knife4j.basic.sign=true
- 在Spring Boot启动类上添加
@EnableKnife4j
注解:
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableKnife4j // 启用Knife4j
public class YourApplication {
public static void main(String[] args) {
SpringApplication.run(YourApplication.class, args);
}
}
- 在Controller中使用Knife4j提供的
@Api
和@ApiOperation
注解来描述API:
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
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(value = "问候接口", notes = "返回一个简单的问候")
@ApiOperationSupport(order = 1, ignore = true)
public String greet() {
return "Hello, Knife4j!";
}
}
完成以上步骤后,你的Spring Boot应用将集成了Knife4j,并可以通过Swagger-UI界面查看和测试API。
评论已关闭