开发框架,帮助后端开发人员做后端接口测试
knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案
引入依赖
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
在配置类中加入knife4j相关配置
@Bean
public Docket docket() {
ApiInfo apiInfo = new ApiInfoBuilder()
.title("xxx项目接口文档")
.version("2.0")
.description("xxx项目接口文档")
.build();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo)
.select()
//指定生成接口需扫描的包
.apis(RequestHandlerSelectors.basePackage("com.ting.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
设置静态资源映射,否则接口文档页面无法显示
否则会把doc.html当作一个controller,但是实际上没有这个controller就会报错404
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}