甩掉接口文档烦恼!Spring Boot 集成 Knife4j,轻松玩转 API 可视化
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class Knife4jConfiguration {
@Bean(value = "defaultApi2")
public Docket defaultApi2() {
Docket docket=new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 指定扫描的包路径
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Knife4j 测试")
.description("Knife4j 测试文档")
.version("1.0")
.build();
}
}
这段代码演示了如何在Spring Boot项目中配置Knife4j,通过Knife4jConfiguration
类创建一个Docket Bean,并设置API信息。这样就可以通过http://<host>:<port>/doc.html
访问API文档,进一步可视化接口信息,提高开发和维护效率。
评论已关闭