溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring?Boot?2.6.x整合Swagger啟動失敗報錯如何解決

發布時間:2022-03-04 13:45:38 來源:億速云 閱讀:718 作者:iii 欄目:開發技術

這篇文章主要介紹了Spring Boot 2.6.x整合Swagger啟動失敗報錯如何解決的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Spring Boot 2.6.x整合Swagger啟動失敗報錯如何解決文章都會有所收獲,下面我們一起來看看吧。

    問題

    Spring Boot 2.6.x版本引入依賴 springfox-boot-starter (Swagger 3.0) 后,啟動容器會報錯:

    Failed to start bean ‘ documentationPluginsBootstrapper ‘ ; nested exception…

    原因

    Springfox 假設 Spring MVC 的路徑匹配策略是 ant-path-matcher,而 Spring Boot 2.6.x版本的默認匹配策略是 path-pattern-matcher,這就造成了上面的報錯。

    解決方案

    方案一(治標)

    在 application.properties 配置文件中修改mvc的匹配策略:

    spring.mvc.pathmatch.matching-strategy=ant-path-matcher

    注意:開始的時候我用這個方法的確可以正常啟動了,但后來我發現此方法在某些服務啟動時會失效!我查了一下才發現這個方法治標不治本,具體如下:

    只有在不使用 Spring Boot 的執行器時,此功能才起作用。

    無論配置的匹配策略如何,執行器將始終使用基于路徑模式的解析 ( 也就是默認策略 ) 。

    如果您想在 Spring Boot 2.6及更高版本中將其與執行器一起使用,則需要對 Springfox 進行更改。

    所以解鈴還須系鈴人吶!要想徹底解決這個bug,需要修改的是 Springfox 。

    方案二(治本)

    這個辦法是我在 github 上找到的,一個大佬提了一個解決方案是將 Springfox 的某 .java 文件復制到自己項目里進行修改,另一個大佬提了一個更好的解決方案,我覺得針不戳,在這里分享一下:

    在你的項目里添加這個 bean :(加在配置類里就可)

    @Bean
    public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
        return new BeanPostProcessor() {
    
            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
                    customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
                }
                return bean;
            }
    
            private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
                List<T> copy = mappings.stream()
                        .filter(mapping -> mapping.getPatternParser() == null)
                        .collect(Collectors.toList());
                mappings.clear();
                mappings.addAll(copy);
            }
    
            @SuppressWarnings("unchecked")
            private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
                try {
                    Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
                    field.setAccessible(true);
                    return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    throw new IllegalStateException(e);
                }
            }
        };
    }

    OK,啟動成功!

    補充:springboot集成swagger,啟動時拋出如下錯誤:

    18:03:03.586 [main] INFO  o.s.b.a.l.ConditionEvaluationReportLoggingListener -
    Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
    18:03:03.601 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter -

    ***************************
    APPLICATION FAILED TO START
    ***************************
    Description:
    Parameter 0 of method linkDiscoverers in org.springframework.hateoas.config.HateoasConfiguration required a single bean, but 15 were found:
        - modelBuilderPluginRegistry: defined in null
        - modelPropertyBuilderPluginRegistry: defined in null
        - typeNameProviderPluginRegistry: defined in null
        - documentationPluginRegistry: defined in null
        - apiListingBuilderPluginRegistry: defined in null
        - operationBuilderPluginRegistry: defined in null
        - parameterBuilderPluginRegistry: defined in null
        - expandedParameterBuilderPluginRegistry: defined in null
        - resourceGroupingStrategyRegistry: defined in null
        - operationModelsProviderPluginRegistry: defined in null
        - defaultsProviderPluginRegistry: defined in null
        - pathDecoratorRegistry: defined in null
        - relProviderPluginRegistry: defined by method 'relProviderPluginRegistry' in class path resource [org/springframework/hateoas/config/HateoasConfiguration.class]
        - linkDiscovererRegistry: defined in null
        - entityLinksPluginRegistry: defined by method 'entityLinksPluginRegistry' in class path resource [org/springframework/hateoas/config/WebMvcEntityLinksConfiguration.class]

    原因:

    swagger版本問題,我本地springboot版本是2.3.1,引用swaggerb版本為2.2.2,導致項目啟動失敗

    解決方案:

    更換swagger版本,我這里換成了2.9.2版本,項目啟動成功

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>

    關于“Spring Boot 2.6.x整合Swagger啟動失敗報錯如何解決”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Spring Boot 2.6.x整合Swagger啟動失敗報錯如何解決”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。

    向AI問一下細節

    免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

    AI

    亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女