1、单体项目集成
mysql mysql-connector-java8.0.29 com.baomidou mybatis-plus-boot-starter3.5.3.1 com.github.xiaoymin knife4j-spring-boot-starter3.0.3
/** * @CreateTime: 2023-10-14 15:33 * @Description: 接口文档配置 * @Version: 1.0 */ @Configuration @EnableSwagger2 public class Knife4jConfiguration { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .useDefaultResponseMessages(false) .apiInfo(apiInfo()) .select().apis(RequestHandlerSelectors.basePackage("org.coding.controller")) .paths(PathSelectors.any()).build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .description("Knife4j API接口文档") .contact(new Contact("demo", "https://xxx.xxx.xxx/demo", "XXX@qq.com")) .version("v1.0") .title("Spring Boot 集成 Validation 参数校验API测试文档").build(); } }
package org.coding.config.knife4j; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; /** * @CreateTime: 2023-10-15 17:16 * @Version: 1.0 */ @Configuration public class PersonWebMvcConfiguration extends WebMvcConfigurationSupport { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**", "classpath:/static/"); registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); super.addResourceHandlers(registry); } }
2、boot3.0集成knife4j4.3.0
com.github.xiaoymin knife4j-openapi3-jakarta-spring-boot-starter4.3.0
@Configuration public class Knife4jConfig { @Bean public OpenAPI springShopOpenAPI() { return new OpenAPI() .info(new Info().title("API接口文档") .description("boot3整合mybatis-plus") .version("v1.0") .license(new License().name("Apache 2.0").url("http://springdoc.org"))) .externalDocs(new ExternalDocumentation() .description("外部文档") .url("https://springshop.wiki.github.org/docs")); } }
# springdoc-openapi项目配置 springdoc: swagger-ui: path: /swagger-ui.html tags-sorter: alpha operations-sorter: alpha api-docs: path: /v3/api-docs group-configs: - group: 'user' paths-to-match: '/**' packages-to-scan: org.coding.java.controller # knife4j的增强配置,不需要增强可以不配 knife4j: enable: true setting: language: zh_cn