原 springboot 启动类
publicstaticvoidmain(String[] args) { ConfigurableApplicationContextapplication=SpringApplication.run(Kinfe4jApplication.class, args); Environmentenv=application.getEnvironment(); Stringhost=null; try { host=InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostExceptione) { logger.error("获取当前服务器HOST失败:{}",e); e.printStackTrace(); } StringspringApplicationName=env.getProperty("spring.application.name"); Stringport=env.getProperty("server.port"); StringcontextPath=env.getProperty("server.servlet.context-path"); StringportAndContextPath=null; StringapplicationName="未获取服务名称"; if (null!=applicationName){ applicationName=springApplicationName; } if (null==contextPath){ portAndContextPath=port; }else { portAndContextPath=port+contextPath; } logger.info("\n----------------------------------------------------------\n\t"+"Application '{}' is Running! Access URLs:\n\t"+"Local: \t\thttp://localhost:{}\n\t"+"External: \thttp://{}:{}\n\t"+"Doc: \thttp://{}:{}/doc.html\n\t"+"----------------------------------------------------------", applicationName, portAndContextPath, host,portAndContextPath, host,portAndContextPath); }
Knife4j 配置类
springboot 启动类上增加注解
BeanValidatorPluginsConfiguration.class) (
knife4j 配置类中扫描所有 api
/*** https://gitee.com/xiaoym/swagger-bootstrap-ui-demo/blob/master/swagger-bootstrap-ui-demo/src/main/java/com/swagger/bootstrap/ui/demo/config/SwaggerConfiguration.java* 默认访问工程下所有api* 注意:web包下子包的类的类名,注解@RequestMapping("/v1")的映射地址,均不能相同* @return*/value="defaultApi") (publicDocketdefaultApi() { //分组/版本,名称StringgroupName="1.0.0版本"; Stringauthor="Huang Min"; StringhomePage="https://www.yuque.com/huangmins/java/hcds4q"; Stringemail="huangmin@exc-led.com"; Stringtitle="多媒体文件处理服务的接口"; Stringdescription="多媒体文件处理服务 RESTful APIs"; StringtermsOfServiceUrl="https://www.yuque.com/huangmins/java/hcds4q"; Stringversion="1.0.0"; Contactcontact=newContact(author, homePage, email); ApiInfoapiInfo=newApiInfoBuilder() .title(title) .description(description) .termsOfServiceUrl(termsOfServiceUrl) .contact(contact) .version(version) .build(); Docketdocket=newDocket(DocumentationType.SWAGGER_2) // 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息) .apiInfo(apiInfo) .groupName(groupName) // 设置哪些接口暴露给Swagger展示 .select() // 扫描所有有注解的api,用这种方式更灵活 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) // 扫描所有 .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); returndocket; }
knife4j 配置类扫描指定包 api
/*** 上海50所的接口测试1** @return*/value="packageApiV2") (publicDocketpackageApiV2() { //分组/版本,名称StringgroupName="V2版本"; Stringauthor="Huang Min"; StringhomePage="https://doc.xiaominfo.com/guide/"; Stringemail="huangmin@exc-led.com"; Stringtitle="多媒体文件处理服务的接口"; Stringdescription="多媒体文件处理服务 RESTful APIs"; StringtermsOfServiceUrl="https://www.yuque.com/huangmins/java/hcds4q"; Stringversion="1.2.0"; StringbasePackage="com.exc.kinfe4j.web.v2"; Knife4jDTOknife4jDTO=newKnife4jDTO(groupName, author, homePage, email, title, description, termsOfServiceUrl, version, basePackage); Docketdocket=createDocket(knife4jDTO); returndocket; } /*** 上海50所的接口测试2** @return*/value="packageApiV3") (publicDocketpackageApiV3() { //分组/版本,名称StringgroupName="50所V3"; Stringauthor="Huang Min"; StringhomePage="https://doc.xiaominfo.com/guide/"; Stringemail="huangmin@exc-led.com"; Stringtitle="多媒体文件处理服务的接口"; Stringdescription="多媒体文件处理服务 RESTful APIs"; StringtermsOfServiceUrl="https://www.yuque.com/huangmins/java/hcds4q"; Stringversion="1.3.0"; StringbasePackage="com.exc.kinfe4j.web.v3"; Knife4jDTOknife4jDTO=newKnife4jDTO(groupName, author, homePage, email, title, description, termsOfServiceUrl, version, basePackage); Docketdocket=createDocket(knife4jDTO); returndocket; } /*** 创建 Docket** @param knife4jDTO* @return Docket*/privateDocketcreateDocket(Knife4jDTOknife4jDTO) { StringgroupName=knife4jDTO.getGroupName(); Stringauthor=knife4jDTO.getAuthor(); StringhomePage=knife4jDTO.getHomePage(); Stringemail=knife4jDTO.getEmail(); Stringtitle=knife4jDTO.getTitle(); Stringdescription=knife4jDTO.getDescription(); StringtermsOfServiceUrl=knife4jDTO.getTermsOfServiceUrl(); Stringversion=knife4jDTO.getVersion(); StringbasePackage=knife4jDTO.getBasePackage(); Contactcontact=newContact(author, homePage, email); ApiInfoapiInfo=newApiInfoBuilder() .title(title) .description(description) .termsOfServiceUrl(termsOfServiceUrl) .contact(contact) .license("exc-led") .licenseUrl("https://www.exc-led.com/") .version(version) .build(); Docketdocket=newDocket(DocumentationType.SWAGGER_2) // 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息) .apiInfo(apiInfo) .groupName(groupName) // 设置哪些接口暴露给Swagger展示 .select() .apis(RequestHandlerSelectors.basePackage(basePackage)) // 扫描所有 .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); returndocket; }
实体类的注解
#实体类的注解,声明被knife4j管理#字段的注解,example:默认参数value="返回码", example="200",notes="返回200,400") (
Controller 接口的注解参数
#接口类的注解#接口的注解#参数列表的注解#每一个参数的注解
Controller 接口的排序
#类的排序,注解ApiSort,参数int类型参数,未添加该注解的排序在后3) (#接口的排序,注解 (order=3),order参数int类型,未添加该注解的排序在后order=2) (
配置文件
spring: application: name: Knife4j-3.x#接口文档系统配置knife4j: #true:启用knife4j增强模式, false:不启用knife4j增强模式enable: true#是否开启生产环境保护策略,生产环境下禁止访问接口文档系统,生产环境设置为true,开发环境设置为false, production: false#对Knife4j提供的资源提供BasicHttp校验,保护文档;production配置为true时,basic认证功能不可用basic: #开启BasicHttp功能enable: falseusername: adminpassword: 123321#是否开启一个默认的跨域配置,该功能配合自定义Host使用,cors: true#cors: true#增强模式配置https://doc.xiaominfo.com/knife4j/enhance.html
pom.xml 配置
<properties><guava.version>27.0.1-jre</guava.version><knife4j.version>3.0.1</knife4j.version><knife4j.annotations.version>1.5.22</knife4j.annotations.version><java.version>1.8</java.version></properties><!-- knife4j 接口文档 --><dependencies><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>${knife4j.version}</version><exclusions><!-- 排除版本冲突的guava --><exclusion><artifactId>guava</artifactId><groupId>com.google.guava</groupId></exclusion><!-- 排除版本冲突的swagger-annotations --><exclusion><artifactId>swagger-annotations</artifactId><groupId>io.swagger</groupId></exclusion></exclusions></dependency><!-- 重新引入指定版本的swagger-annotations --><dependency><groupId>io.swagger</groupId><artifactId>swagger-annotations</artifactId><version>${knife4j.annotations.version}</version></dependency><!-- 引入guava并发编程框架 --><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>${guava.version}</version></dependency></dependencies>
GitHub 工程示例
下载链接
https://github.com/huangmins/knife4j.git
启动工程
下载工程后, 导入 idea, 启动成功, 如下图所示:
knife4j 系统
进入系统页面
切换接口分组
下载查看离线接口文档
下载指定分组的离线接口文档
下载 typora 工具📎typora-setup-x64.zip
下载 typora 工具📎typora-setup-x64.zip
查看离线接口文档