SpringBoot 2.6.13整合knif4j 3.0.3

SpringBoot 2.6.13整合knif4j 3.0.3


 
 
  1. <dependency >
  2. <groupId >com.github.xiaoymin < /groupId >
  3. <artifactId >knife 4j-spring-boot-starter < /artifactId >
  4. <version > 3.0.3 < /version >
  5. < /dependency >

第二步、配置swagger


 
 
  1. package com.xmx.config;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.config.BeanPostProcessor;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.util.ReflectionUtils;
  7. import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
  8. import springfox.documentation.builders.ApiInfoBuilder;
  9. import springfox.documentation.builders.PathSelectors;
  10. import springfox.documentation.builders.RequestHandlerSelectors;
  11. import springfox.documentation.service.ApiInfo;
  12. import springfox.documentation.spi.DocumentationType;
  13. import springfox.documentation.spring.web.plugins.Docket;
  14. import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider;
  15. import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;
  16. import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
  17. import java.lang.reflect.Field;
  18. import java.util.List;
  19. import java.util.stream.Collectors;
  20. @Configuration
  21. @EnableSwagger2WebMvc
  22. public class Knife4jConfig {
  23. @Bean
  24. public Docket createRestApi () {
  25. return new Docket(DocumentationType.SWAGGER_2)
  26. // apiInfo():配置 API 的一些基本信息,比如:文档标题title,文档描述description,文档版本号version
  27. .apiInfo(apiInfo())
  28. // select():生成 API 文档的选择器,用于指定要生成哪些 API 文档
  29. .select()
  30. // apis():指定要生成哪个包下的 API 文档
  31. .apis(RequestHandlerSelectors.basePackage( "com.xmx.controller"))
  32. // paths():指定要生成哪个 URL 匹配模式下的 API 文档。这里使用 PathSelectors.any(),表示生成所有的 API 文档。
  33. .paths(PathSelectors.any())
  34. .build();
  35. }
  36. private static final String API_TILE= "xx项目";
  37. //文档信息配置
  38. private ApiInfo apiInfo () {
  39. return new ApiInfoBuilder()
  40. // 文档标题
  41. .title(API_TILE)
  42. // 文档描述信息
  43. .description( "xxx在线API文档")
  44. // 文档版本号
  45. .version( "1.0")
  46. .build();
  47. }
  48. @Bean
  49. public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor () {
  50. return new BeanPostProcessor() {
  51. @Override
  52. public Object postProcessAfterInitialization (Object bean, String beanName) throws BeansException {
  53. if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
  54. customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
  55. }
  56. return bean;
  57. }
  58. private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings (List<T> mappings) {
  59. List<T> copy = mappings.stream()
  60. .filter(mapping -> mapping.getPatternParser() == null)
  61. .collect(Collectors.toList());
  62. mappings.clear();
  63. mappings.addAll(copy);
  64. }
  65. @SuppressWarnings("unchecked")
  66. private List<RequestMappingInfoHandlerMapping> getHandlerMappings (Object bean) {
  67. try {
  68. Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
  69. field.setAccessible( true);
  70. return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
  71. } catch (IllegalArgumentException | IllegalAccessException e) {
  72. throw new IllegalStateException(e);
  73. }
  74. }
  75. };
  76. }
  77. }

第三步、在application.yaml文件中配置

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

 第四步、访问:http://localhost:8002/doc.html访问文档

 

文章知识点与官方知识档案匹配,可进一步学习相关知识
Java技能树首页概览 126137 人正在系统学习中
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值