来源:自学PHP网 时间:2020-09-27 14:16 作者:小飞侠 阅读:次
[导读] SpringBoot中整合knife4j接口文档的实践...
今天带来SpringBoot中整合knife4j接口文档的实践教程详解
在项目开发中,web项目的前后端分离开发,APP开发,需要由前后端工程师共同定义接口,编写接口文档,之后大家都根据这个接口文档进行开发,到项目结束前都要一直维护 接口文档使得项目开发过程中前后端工程师有一个统一的文件进行沟通交流开发,项目维护中或者项目人员更迭,方便后期人员查看、维护 一、界面先赏 1、首页 2、接口文档 3、调试 二、整合 knife4j 1、引入 maven 依赖 <!-- knife4j接口文档 start --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.2</version> </dependency> <!-- 避免版本冲突 --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>29.0-jre</version> </dependency> 一般情况我们只需要引入 knife4j 的依赖即可,但是有时会出现 guava 的版本冲突,所以,我们把 guava 一起引入进来 2、knife4j 配置文件 创建 Knife4jConfig 文件 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; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * knife4j 配置 * * @Author Lizhou */ @Configuration @EnableSwagger2 public class Knife4jConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.zyxx")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("SpringBoot项目 后台服务API接口文档") .description("使用 knife4j 搭建的后台服务API接口文档") .termsOfServiceUrl("http://localhost:8080/") .contact("lizhou") .version("1.0.0") .build(); } } 整体配置与 Swagger2 几乎一致,扫描 controller 所在的包 3、启动类 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; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * knife4j 配置 * * @Author Lizhou */ @Configuration @EnableSwagger2 public class Knife4jConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.zyxx")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("SpringBoot项目 后台服务API接口文档") .description("使用 knife4j 搭建的后台服务API接口文档") .termsOfServiceUrl("http://localhost:8080/") .contact("lizhou") .version("1.0.0") .build(); } } 我们需要开放其静态资源的访问,启动类实现 WebMvcConfigurer 接口,重写 addResourceHandlers 方法 4、访问文档 启动项目,访问路径http://localhost:8080/doc.html 三、注意 访问时,如果提示 knife4j 文档异常,检查下自己的拦截器是否没有放开 knife4j 所需要的请求
|
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com