# SpringBoot 整合 knife4j 框架 (可生成离线接口文档),并设置接口请求头 token 默认值
功能和 swagger 类似
官网地址:https://doc.xiaominfo.com/knife4j/
引入依赖
1 2 3 4 5
| <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.7</version> </dependency>
|
Knife4jConfig .java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| 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;
@Configuration @EnableSwagger2 public class Knife4jConfig {
@Bean public Docket docket(Environment environment) { Profiles profiles=Profiles.of("dev"); 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()) .enable(flag) .groupName("yvioo") .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .build() .globalOperationParameters(pars); }
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 基本类似
1 2 3 4 5 6 7 8 9
| @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 就需要增加资源映射 不然会失效
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @Configuration public class WebMvcConfigurer extends 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
# 关于我
Brath 是一个热爱技术的 Java 程序猿,公众号「InterviewCoder」定期分享有趣有料的精品原创文章!
非常感谢各位人才能看到这里,原创不易,文章如果有帮助可以关注、点赞、分享或评论,这都是对我的莫大支持!