SpringBoot整合knife4j框架(可生成离线接口文档),并设置接口请求头token默认值
功能和swagger类似
官网地址:https://doc.xiaominfo.com/knife4j/
这个框架可以设置返回字段的描述
引入依赖
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.7</version> </dependency>
Knife4jConfig .java
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.core.env.Profiles; import springfox.documentation.builders.ParameterBuilder; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.schema.ModelRef; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.service.Parameter; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; import java.util.List; /** * @author yvioo。 */ @Configuration @EnableSwagger2 //开启Swagger2 public class Knife4jConfig { /** * 配置Swagger的Docket的bean实例 * @return */ @Bean public Docket docket(Environment environment) { //设置只在开发中环境中启动swagger Profiles profiles=Profiles.of("dev"); //表示如果现在是dev环境,则返回true 开启swagger boolean flag=environment.acceptsProfiles(profiles); /*添加接口请求头参数配置 没有的话 可以忽略*/ ParameterBuilder tokenPar = new ParameterBuilder(); List<Parameter> pars = new ArrayList<>(); tokenPar.name("token").description("令牌").defaultValue("设置token默认值").modelRef(new ModelRef("string")).parameterType("header").required(false).build(); pars.add(tokenPar.build()); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) //是否启动swagger 默认启动 .enable(flag) //所在分组 .groupName("yvioo") .select() //指定扫描的包路径 .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) //指定扫描的请求,这里表示扫描 /hello/ 的请求 //.paths(PathSelectors.ant("/hello/**")) .build() .globalOperationParameters(pars); } /** * 配置ApiInfo信息 * @return */ private ApiInfo apiInfo() { //作者信息 Contact author = new Contact("yvioo", "https://www.cnblogs.com/pxblog/", "111@qq.com"); return new ApiInfo( "Knife4j测试", "Knife4j描述", "1.0", "urn:tos", author, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList() ); } }
控制器的写法和swagger基本类似
@Api(tags = "首页模块") @RestController public class IndexController { @ApiImplicitParam(name = "name",value = "姓名",required = true) @ApiOperation(value = "向客人问好") @GetMapping("/sayHi") public ResponseEntity<String> sayHi(@RequestParam(value = "name")String name){ return ResponseEntity.ok("Hi:"+name); } }
但是如果有其他配置继承了 WebMvcConfigurationSupport 就需要增加资源映射 不然会失效
@Configuration public class WebMvcConfigurer extends WebMvcConfigurationSupport { /** * 发现如果继承了WebMvcConfigurationSupport, 需要重新指定静态资源 * */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations( "classpath:/static/"); registry.addResourceHandler("doc.html").addResourceLocations( "classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations( "classpath:/META-INF/resources/webjars/"); super.addResourceHandlers(registry); } }
效果
离线接口文档
浏览器访问
使用dev环境 启动项目后 浏览器打开 http://localhost:8081/doc.html#/ 我这里用的端口是8081
整合swagger框架参考:https://www.cnblogs.com/pxblog/p/12942825.html
-----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------
(蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了