SpringBoot自动生成Api文档 | 您所在的位置:网站首页 › springboot接口管理 › SpringBoot自动生成Api文档 |
一、准备 项目准备SpringBoot 版本: Java : JDk 1.8Springboot : 2.7.11knife4j : 2.0.5实体类控制类 二、Knife4j官方文档:Knife4j · 集Swagger2及OpenAPI3为一体的增强解决方案. | Knife4j 作用 前后端分离项目中做到快速开发,生成api文档,大大提高开发效率 操作: 使用Maven导入需要用到的包 com.github.xiaoymin knife4j-dependencies 2.0.5 pom import com.github.xiaoymin knife4j-spring-boot-starterapplication.yml文件配置 在使用过程中遇到问题最多的地方就是这个文件没有配置 mvc: pathmatch: matching-strategy: ant_path_matcherKnife4jConfig工具类(根据官方demo改写) /* * Copyright (C) 2018 Zhejiang xiaominfo Technology CO.,LTD. * All rights reserved. * Official Web Site: http://www.xiaominfo.com. * Developer Web Site: http://open.xiaominfo.com. */ package com.wms.common; import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; 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; /** * @author [email protected] * 2020/09/18 11:04 * @since:knife4j-spring-boot2-demo 1.0 */ @EnableSwagger2 @EnableKnife4j @Configuration public class SwaggerConfig { @Bean(value = "defaultApi2") public Docket defaultApi2() { Docket docket=new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) //分组名称 .groupName("wmsApi接口测试") .select() //这里指定Controller扫描包路径 .apis(RequestHandlerSelectors.basePackage("com.wms.controller")) .paths(PathSelectors.any()) .build(); return docket; } private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("wmsApi") .description("仓库管理系统接口测试") .termsOfServiceUrl("http://www.group.com/") .contact("[email protected]") .version("1.0") .build(); } } 三、问题与解决方案 【问题1】org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException 1. 问题原因: Caused by: java.lang.NullPointerException: null 2. 分析: 查了很多资料没有找到问题具体所在,最后结合官方demo找到了问题。application.yml文件中配置有问题 3. 解决方案: 在applicaiton.yml文件中添加代码 mvc: pathmatch: matching-strategy: ant_path_matcher 四、参考文档 Knife4j官方文档:Knife4j · 集Swagger2及OpenAPI3为一体的增强解决方案. | Knife4j |
CopyRight 2018-2019 实验室设备网 版权所有 |