当前位置:   article > 正文

SpringBoot3.0.0-SNAPSHOT与swagger3不兼容问题解决_swagger 3.0.0不支持spring3.0.0

swagger 3.0.0不支持spring3.0.0

        最近将SpringBoot升级到了SpringBoot3.0.0-SNAPSHOT版本,我项目里面集成的swagger是springfox-swagger2的3.0.0版本。maven clean和install都没问题。可是运行项目时程序报错。报错如下:

  1. Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
  2. 2022-07-25T17:33:19.262+08:00 ERROR 9612 --- [ main] o.s.boot.SpringApplication : Application run failed
  3. org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'
  4. at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-6.0.0-20220722.142958-638.jar:6.0.0-SNAPSHOT]
  5. at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-6.0.0-20220722.142958-638.jar:6.0.0-SNAPSHOT]
  6. at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
  7. at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-6.0.0-20220722.142958-638.jar:6.0.0-SNAPSHOT]
  8. at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-6.0.0-20220722.142958-638.jar:6.0.0-SNAPSHOT]
  9. at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:943) ~[spring-context-6.0.0-20220722.142958-638.jar:6.0.0-SNAPSHOT]
  10. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:595) ~[spring-context-6.0.0-20220722.142958-638.jar:6.0.0-SNAPSHOT]
  11. at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.0.0-20220725.081705-662.jar:3.0.0-SNAPSHOT]
  12. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:735) ~[spring-boot-3.0.0-20220725.081705-662.jar:3.0.0-SNAPSHOT]
  13. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-3.0.0-20220725.081705-662.jar:3.0.0-SNAPSHOT]
  14. at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-3.0.0-20220725.081705-662.jar:3.0.0-SNAPSHOT]
  15. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1307) ~[spring-boot-3.0.0-20220725.081705-662.jar:3.0.0-SNAPSHOT]
  16. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1296) ~[spring-boot-3.0.0-20220725.081705-662.jar:3.0.0-SNAPSHOT]
  17. at com.lvxiaosha.Application.main(Application.java:22) ~[classes/:na]
  18. Caused by: java.lang.NullPointerException: Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns()" because "this.condition" is null
  19. at springfox.documentation.spring.web.WebMvcPatternsRequestConditionWrapper.getPatterns(WebMvcPatternsRequestConditionWrapper.java:56) ~[springfox-spring-webmvc-3.0.0.jar:3.0.0]
  20. at springfox.documentation.RequestHandler.sortedPaths(RequestHandler.java:113) ~[springfox-core-3.0.0.jar:3.0.0]
  21. at springfox.documentation.spi.service.contexts.Orderings.lambda$byPatternsCondition$3(Orderings.java:89) ~[springfox-spi-3.0.0.jar:3.0.0]
  22. at java.base/java.util.Comparator.lambda$comparing$77a9974f$1(Comparator.java:473) ~[na:na]
  23. at java.base/java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) ~[na:na]
  24. at java.base/java.util.TimSort.sort(TimSort.java:234) ~[na:na]
  25. at java.base/java.util.Arrays.sort(Arrays.java:1307) ~[na:na]
  26. at java.base/java.util.ArrayList.sort(ArrayList.java:1721) ~[na:na]
  27. at java.base/java.util.stream.SortedOps$RefSortingSink.end(SortedOps.java:392) ~[na:na]
  28. at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:na]
  29. at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:na]
  30. at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:na]
  31. at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:na]
  32. at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:510) ~[na:na]
  33. at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) ~[na:na]
  34. at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921) ~[na:na]
  35. at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
  36. at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682) ~[na:na]
  37. at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:81) ~[springfox-spring-webmvc-3.0.0.jar:3.0.0]
  38. at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197) ~[na:na]
  39. at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625) ~[na:na]
  40. at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509) ~[na:na]
  41. at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) ~[na:na]
  42. at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921) ~[na:na]
  43. at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
  44. at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682) ~[na:na]
  45. at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.withDefaults(AbstractDocumentationPluginsBootstrapper.java:107) ~[springfox-spring-web-3.0.0.jar:3.0.0]
  46. at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.buildContext(AbstractDocumentationPluginsBootstrapper.java:91) ~[springfox-spring-web-3.0.0.jar:3.0.0]
  47. at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.bootstrapDocumentationPlugins(AbstractDocumentationPluginsBootstrapper.java:82) ~[springfox-spring-web-3.0.0.jar:3.0.0]
  48. at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:100) ~[springfox-spring-web-3.0.0.jar:3.0.0]
  49. at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-6.0.0-20220722.142958-638.jar:6.0.0-SNAPSHOT]
  50. ... 13 common frames omitted

        新增Swagger2配置类,内容如下:

  1. import org.springframework.beans.BeansException;
  2. import org.springframework.beans.factory.config.BeanPostProcessor;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.util.ReflectionUtils;
  6. import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
  7. import springfox.documentation.builders.ApiInfoBuilder;
  8. import springfox.documentation.builders.PathSelectors;
  9. import springfox.documentation.builders.RequestHandlerSelectors;
  10. import springfox.documentation.service.ApiInfo;
  11. import springfox.documentation.service.Contact;
  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.EnableSwagger2;
  17. import java.lang.reflect.Field;
  18. import java.util.List;
  19. import java.util.stream.Collectors;
  20. @Configuration
  21. @EnableSwagger2
  22. public class Swagger2 {
  23. // http://localhost:8088/swagger-ui.html 原路径
  24. // http://localhost:8088/doc.html 原路径
  25. // 配置swagger2核心配置 docket
  26. @Bean
  27. public Docket createRestApi() {
  28. return new Docket(DocumentationType.SWAGGER_2) // 指定api类型为swagger2
  29. .apiInfo(apiInfo()) // 用于定义api文档汇总信息
  30. .select()
  31. .apis(RequestHandlerSelectors
  32. .basePackage("com.lvxiaosha.controller")) // 指定controller包
  33. .paths(PathSelectors.any()) // 所有controller
  34. .build();
  35. }
  36. private ApiInfo apiInfo() {
  37. return new ApiInfoBuilder()
  38. .title("吕小傻学习项目接口api") // 文档页标题
  39. .contact(new Contact("lvxiaosha",
  40. "https://www.lvxiaosha.com",
  41. "abc@lvxiaosha.com")) // 联系人信息
  42. .description("吕小傻学习项目接口api文档") // 详细信息
  43. .version("1.0.0") // 文档版本号
  44. .termsOfServiceUrl("https://www.lvxiaosha.com") // 网站地址
  45. .build();
  46. }
  47. /**
  48. * 增加如下配置可解决Spring Boot 6.x 与Swagger 3.0.0 不兼容问题
  49. **/
  50. @Bean
  51. public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
  52. return new BeanPostProcessor() {
  53. @Override
  54. public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  55. if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
  56. customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
  57. }
  58. return bean;
  59. }
  60. private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
  61. List<T> copy = mappings.stream()
  62. .filter(mapping -> mapping.getPatternParser() == null)
  63. .collect(Collectors.toList());
  64. mappings.clear();
  65. mappings.addAll(copy);
  66. }
  67. @SuppressWarnings("unchecked")
  68. private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
  69. try {
  70. Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
  71. field.setAccessible(true);
  72. return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
  73. } catch (IllegalArgumentException | IllegalAccessException e) {
  74. throw new IllegalStateException(e);
  75. }
  76. }
  77. };
  78. }
  79. }

        再次启动项目,报错解决。

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

闽ICP备14008679号