当前位置:   article > 正文

Springboot3.0.0+集成SpringDoc并配置knife4j的UI_spring boot3 集成springdoc

spring boot3 集成springdoc

环境:JDK17,Springboot3+,springdoc2+,knife4j 4+

Springdoc本身也是集成了Swagger3,而knife4j美化了Swagger3的UI

Knife4j官网:

快速开始 | Knife4j

Springdoc官网

OpenAPI 3 Library for spring-boot

1.pom配置

由于此knife4j内依赖了SpringDoc,因此不用另外引入springdoc的依赖

  1. <dependency>
  2. <groupId>com.github.xiaoymin</groupId>
  3. <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
  4. <version>4.3.0</version>
  5. </dependency>

springdoc依赖(无需引入),亲测引入也不会冲突

  1. <dependency>
  2. <groupId>org.springdoc</groupId>
  3. <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
  4. <version>2.2.0</version>
  5. </dependency>

2.application.yml配置

  1. springdoc:
  2. swagger-ui:
  3. path: /swagger-ui.html
  4. tags-sorter: alpha
  5. operations-sorter: alpha
  6. enabled: true
  7. api-docs:
  8. path: /v3/api-docs
  9. enabled: true
  10. group-configs:
  11. group: platform
  12. paths-to-match: /**
  13. packages-to-scan: com.license4j.license
  14. knife4j:
  15. enable: true
  16. setting:
  17. language: zh_cn

3.代码配置

addInterceptors主要放开了文档的路径访问

addResourceHandlers主要设置了接口文档静态资源路径

addResourceHandlers配置很重要,不配置会导致接口文档404,后台也会报异常

No mapping for GET /doc.html

  1. import jakarta.annotation.Resource;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  4. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  5. import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. @Configuration
  9. public class WebMvcRegistrationsConfig extends WebMvcConfigurationSupport {
  10. @Resource
  11. private LicenseInterceptor licenseInterceptor;
  12. public WebMvcRegistrationsConfig(LicenseInterceptor licenseInterceptor) {
  13. this.licenseInterceptor = licenseInterceptor;
  14. }
  15. /**
  16. * 拦截器配置
  17. * @param registry
  18. */
  19. @Override
  20. public void addInterceptors(InterceptorRegistry registry) {
  21. // 无需拦截的接口集合
  22. List<String> ignorePath = new ArrayList<>();
  23. // swagger
  24. ignorePath.add("/swagger-resources/**");
  25. ignorePath.add("/doc.html");
  26. ignorePath.add("/v3/**");
  27. ignorePath.add("/webjars/**");
  28. ignorePath.add("/springdoc/**");
  29. ignorePath.add("/static/**");
  30. ignorePath.add("/templates/**");
  31. ignorePath.add("/error");
  32. ignorePath.add("/cipher/check");
  33. ignorePath.add("/manager/login");
  34. ignorePath.add("/swagger-ui.html");
  35. //先拦截认证,再拦截授权
  36. registry.addInterceptor(licenseInterceptor).addPathPatterns("/**").excludePathPatterns(ignorePath);
  37. }
  38. /**
  39. * 静态资源配置
  40. * @param registry
  41. */
  42. @Override
  43. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  44. registry.addResourceHandler("doc.html")
  45. .addResourceLocations("classpath:/META-INF/resources/");
  46. registry.addResourceHandler("swagger-ui.html")
  47. .addResourceLocations("classpath:/META-INF/resources/");
  48. registry.addResourceHandler("/webjars/**")
  49. .addResourceLocations("classpath:/META-INF/resources/webjars/");
  50. }
  51. }

swagger基本配置

  1. import io.swagger.v3.oas.models.ExternalDocumentation;
  2. import io.swagger.v3.oas.models.OpenAPI;
  3. import io.swagger.v3.oas.models.info.Info;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. @Configuration
  7. public class SwaggerConfig {
  8. private Info info(){
  9. return new Info()
  10. .title("API接口文档")
  11. .description("")
  12. .version("v1.0.0");
  13. }
  14. @Bean
  15. public OpenAPI springShopOpenAPI() {
  16. return new OpenAPI()
  17. .info(info())
  18. .externalDocs(externalDocumentation());
  19. }
  20. }

4.访问路径 ip+端口+doc.html

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号