SpringBoot整合knife4j
SpringBoot整合knife4j
目前来说比较好用,UI比较好看的接口文档
配置完成访问显示404需添加WebMvcConfigurationConfig.java
依赖
<!-- springboot为2.7.12 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.8</version>
</dependency>
Knife4jConfig.java
package com.xust.config;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.beans.factory.annotation.Autowired;
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.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
/**
* @BelongsProject: server-toolbox
* @BelongsPackage: com.xust.config
* @Author: xpx
* @Email: 2436846019@qq.com
* @CreateTime: 2023-08-16 21:06
* @Description: Swagger配置类
* @Version: 1.0
*/
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {
private final OpenApiExtensionResolver openApiExtensionResolver;
@Autowired
public Knife4jConfig(OpenApiExtensionResolver openApiExtensionResolver) {
this.openApiExtensionResolver = openApiExtensionResolver;
}
@Bean(value = "defaultApi1")
public Docket defaultApi2() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.title("ToolBox-API接口文档 1.0")
.description("个人调试")
.termsOfServiceUrl("xxxxxxxxxxxxxx")
.version("1.0")
.contact(new Contact("xpx", "http://xust.com", "123@qq.com"))
.build())
//分组名称
.groupName("RESTful 1.0")
.extensions(openApiExtensionResolver.buildSettingExtensions())
.select()
//这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.xust.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
}
WebMvcConfigurationConfig.java
package com.xust.config;
/**
* @BelongsProject: server-toolbox
* @BelongsPackage: com.xust.config
* @Author: xpx
* @Email: 2436846019@qq.com
* @CreateTime: 2023-08-18 16:12
* @Description: TODO
* @Version: 1.0
*/
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class WebMvcConfigurationConfig extends WebMvcConfigurationSupport {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
application.yaml
knife4j:
enable: true
production: false
setting:
enableSwaggerModels: false
swaggerModelName: 平台模型对象实体
enableFooter: false
enableFooterCustom: true
footerCustomContent:
enableSearch: false
enableOpenApi: false
enableRequestCache: false