Spring Boot项目集成Knife4j接口文档的实例代码
knife4j
就相当于是swagger
的升级版,对于我来说,它比swagger
要好用得多
1、在pom.xml引入依赖包
<!-- swagger配置依赖knife4j --> <dependency> <groupid>com.github.xiaoymin</groupid> <artifactid>knife4j-spring-boot-starter</artifactid> <version>2.0.9</version> </dependency>
2、创建knife4j配置文件
package com.yuyun.config; import io.swagger.annotations.apioperation; import io.swagger.models.auth.in; 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.service.apikey; import springfox.documentation.service.contact; import springfox.documentation.service.securityscheme; import springfox.documentation.spi.documentationtype; import springfox.documentation.spring.web.plugins.docket; import springfox.documentation.swagger2.annotations.enableswagger2webmvc; import java.util.arraylist; import java.util.list; /** * @author hyh */ @configuration @enableswagger2webmvc public class knife4jconfiguration { @bean(value = "defaultapi2") public docket defaultapi2() { docket docket = new docket(documentationtype.swagger_2) // 是否启用swagger .enable(true) //分组名称 .groupname("1.0版本") // 用来创建该api的基本信息,展示在文档的页面中(自定义展示的信息) .apiinfo(apiinfo()) // 设置哪些接口暴露给swagger展示 .select() // 扫描所有有注解的api,用这种方式更灵活 .apis(requesthandlerselectors.withmethodannotation(apioperation.class)) //指定controller扫描包路径 // .apis(requesthandlerselectors.basepackage("com.yuyun.controller")) // 扫描所有 // .apis(requesthandlerselectors.any()) .build(); return docket; } private apiinfo apiinfo() { string name = "雨云"; string url = "https://www.xxx.com/"; string email = "1873591403@qq.com"; contact contact = new contact(name, url, email); return new apiinfobuilder() .title("api接口文档") .description("api接口文档描述") .termsofserviceurl("https://www.xx.com/") .contact(contact) .version("1.0.1") .build(); } }
注意:如果出现错误failed to start bean 'documentationpluginsbootstrapper'; nested exception is java.lang.nullpointerexception
是因为springboot版本高了,将版本降下去或者在application.yml
添加如下内容即可解决该错误
spring: mvc: pathmatch: matching-strategy: ant_path_matcher
项目运行后,访问ip
+端口号
+/doc.html
,比如http://localhost:8110/doc.html。效果如图
3、使用knife4j注解
(1)在实体类中使用
@apimodel
放在在响应实体类上,用于描述该类
@apimodelproperty
描述该响应类的属性
/** * 企业信息表 * * @author * @since 1.0.0 2021-12-17 */ @data @apimodel(value = "企业信息表") @tablename("company") public class companydto implements serializable { private static final long serialversionuid = 1l; /** * 主键 */ @apimodelproperty(value = "主键") private long id; /** * 企业名称 */ @apimodelproperty(value = "企业名称") private string companyname; /** * 简介 */ @apimodelproperty(value = "简介") private string description; }
(2)在controller层使用
@restcontroller @requestmapping("company") @api(tags = "企业信息表") public class companycontroller { @autowired private companyservice companyservice; @getmapping("getlist") @apioperation("根据条件获取数据") @apiimplicitparams({ @apiimplicitparam(name = "id", value = "id", paramtype = "query", required = true, datatype = "string"), @apiimplicitparam(name = "name", value = "名称", paramtype = "query", required = true, datatype = "string") }) public result<list<companydto>> getlist(@apiparam(name = "address", value = "地址", required = true) string address) { list<companydto> companylist = companyservice.list(); return new result<list<companydto>>().success(companylist); } }
还有其他一些注解,用到再了解
4、全局参数
在实际项目中访问接口都添加了权限,每次访问都要带一个请求头参数token。全局参数就是为了方便传一个固定的参数。当添加全局参数后,所有的接口都会带上该参数。
第一种
在配置文件中加入
private list<securityscheme> securityschemes() { list<securityscheme> apikeylist = new arraylist<securityscheme>(); apikeylist.add(new apikey("authorization", "authorization", in.header.tovalue())); return apikeylist; }
在defaultapi2()
方法内引用
.securityschemes(securityschemes())
最后配置文件中的内容:
@configuration @enableswagger2webmvc public class knife4jconfiguration { @bean(value = "defaultapi2") public docket defaultapi2() { docket docket = new docket(documentationtype.swagger_2) // 是否启用swagger .enable(true) //分组名称 .groupname("1.0版本") // 用来创建该api的基本信息,展示在文档的页面中(自定义展示的信息) .apiinfo(apiinfo()) // 设置哪些接口暴露给swagger展示 .select() // 扫描所有有注解的api,用这种方式更灵活 .apis(requesthandlerselectors.withmethodannotation(apioperation.class)) //指定controller扫描包路径 // .apis(requesthandlerselectors.basepackage("com.yuyun.controller")) // 扫描所有 // .apis(requesthandlerselectors.any()) .paths(pathselectors.any()) .build() /* 设置安全模式,swagger可以设置访问token */ .securityschemes(securityschemes()) .securitycontexts(securitycontexts()) .pathmapping("/"); return docket; } private apiinfo apiinfo() { string name = "雨云"; string url = "https://www.xxx.com/"; string email = "1873591403@qq.com"; contact contact = new contact(name, url, email); return new apiinfobuilder() .title("api接口文档") .description("api接口文档描述") .termsofserviceurl("https://www.xx.com/") .contact(contact) .version("1.0.1") .build(); } /** * 安全模式,这里指定token通过authorization头请求头传递 */ private list<securityscheme> securityschemes() { list<securityscheme> apikeylist = new arraylist<securityscheme>(); apikeylist.add(new apikey("authorization", "authorization", "header")); return apikeylist; } /** * 安全上下文 */ private list<securitycontext> securitycontexts() { list<securitycontext> securitycontexts = new arraylist<>(); securitycontexts.add( securitycontext.builder() .securityreferences(defaultauth()) .build()); return securitycontexts; } /** * 默认的安全上引用 */ private list<securityreference> defaultauth() { authorizationscope authorizationscope = new authorizationscope("global", "accesseverything"); authorizationscope[] authorizationscopes = new authorizationscope[1]; authorizationscopes[0] = authorizationscope; list<securityreference> securityreferences = new arraylist<>(); securityreferences.add(new securityreference("authorization", authorizationscopes)); return securityreferences; } }
效果:菜单上多了一个authorize
,在参数值中添加上信息
刷新一下,再打开接口就会发现多了个请求头部
第二种
直接在菜单文档管理
→全局参数设置
,然后添加参数:
再打开接口就会发现请求头参数加上了
源码地址:https://gitee.com/hyh17808770899/spring-boot/tree/master/springboot-03
到此这篇关于spring boot项目集成knife4j接口文档的文章就介绍到这了,更多相关spring boot集成knife4j接口文档内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!