springboot集成knife4j2.0.8实现⾃定义md⽂档及权限控制
⼀、原由
最近在做⼀个⼩项⽬的开⼊API,⽂档是⽤vuepress写的静态md并发布。因为是项⽬刚起步,对接的接⼝也会经常改动,每次改动都要修改⽂档感觉太⿇烦,就想到⽤knife4j来代替。但外部使⽤的接⼝和内部使⽤不⼀样,需要⼀些接⼊引导说明,⽐如API接⼊步骤、鉴权说明…等
重新翻了knife4j的⽂档发现2.x版本⾃2.0.7之后可以使⽤⾃定义⽂档的功能,⾃定义⽂档正好也是我喜欢的Markdown⽂件,再加⼀个权限验证,⽬前是很好的解决了我的问题。
效果:
⼆、开始
knife4j2.x版如果想实现⾃定义Markdown⽂档需要将版本升级2.0.7及以上
本次测试使⽤的springboot版本 2.0.3.RELEASE
需要⽤到的依赖
//swagger版本⽀持
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.10.5'
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.10.5'
compile 'io.springfox:springfox-spring-webmvc:2.10.5'
//swagger注解⽆默认值错误忽略
compile group: 'io.swagger', name: 'swagger-annotations', version: '1.5.22'
compile group: 'io.swagger', name: 'swagger-models', version: '1.5.22'
//knife2.x版本
compile group: 'com.github.xiaoymin', name: 'knife4j-spring-boot-starter', version: '2.0.8'
/
/升级spring-plugin-core,否则会有冲突
compile 'org.springframework.plugin:spring-plugin-core:2.0.0.RELEASE'
SwaggerConfiguration
ly.fig;
import com.fasterxml.classmate.TypeResolver;
import com.github.xiaoymin.sion.OpenApiExtensionResolver;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import t.annotation.Bean;
import t.annotation.Configuration;
import t.annotation.Import;
import springfox.figuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
@Configuration
@EnableSwagger2WebMvcspringboot结构
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {
private final TypeResolver typeResolver;
private final OpenApiExtensionResolver openApiExtensionResolver;
@Autowired
public SwaggerConfiguration(TypeResolver typeResolver, OpenApiExtensionResolver openApiExtensionResolver){ peResolver = typeResolver;
this.openApiExtensionResolver = openApiExtensionResolver;
}
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("penapi.api"))//扫描包
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))//扫描在API注解的contorller
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))//扫描带ApiOperation注解的⽅法.paths(PathSelectors.any())
.build()
.extensions(openApiExtensionResolver.buildExtensions("2.X版本"));
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("昂⽴课堂(开放API)")
.description("昂⽴课堂开放平台")
.version("v2.x")
.license("Apache License Version 2.0")
.licenseUrl("/licenses/LICENSE-2.0")
.contact(new Contact("朱增亮","","zhuzengliang@onlyedu"))
.build();
}
}
server:
port:8080
knife4j:
enable:true
# ⽣产环境屏蔽
# production: true
# 开启Swagger的Basic认证功能,默认是false basic:
enable:true
username: only
password: only
documents:
-
group: 2.X版本
name:另外⽂档分组请看这⾥
locations: classpath:markdown/*
-group: 2.X版本
name:接⼊⽅法
locations: classpath:markdown/api.md
⽂档结构位置
源码
三、坑
当你⼀切准备就绪时就报如下错误
这⾥需要升级spring核⼼插件
compile 'org.springframework.plugin:spring-plugin-core:2.0.0.RELEASE'
原创⽂章未经本⼈许可,不得⽤于商业⽤途及传统媒体。转载请注明出处,否则属于侵权⾏为,谢谢合作!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论