最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot 2.6.x整合springfox 3.0報錯問題及解決方案

 更新時間:2024年01月10日 10:06:01   作者:kkorkk  
這篇文章主要介紹了SpringBoot 2.6.x整合springfox 3.0報錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

一、問題現(xiàn)象

SpringBoot版本:2.6.4

springfox swagger版本:

<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-boot-starter</artifactId>
	<version>3.0.0</version>
</dependency>

啟動之后報錯:

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.16.jar:5.3.16]
    at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.16.jar:5.3.16]
    at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.16.jar:5.3.16]
    at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_202]
    at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.16.jar:5.3.16]
    at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.16.jar:5.3.16]
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.16.jar:5.3.16]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.16.jar:5.3.16]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.4.jar:2.6.4]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740) [spring-boot-2.6.4.jar:2.6.4]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415) [spring-boot-2.6.4.jar:2.6.4]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-2.6.4.jar:2.6.4]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312) [spring-boot-2.6.4.jar:2.6.4]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) [spring-boot-2.6.4.jar:2.6.4]
    at com.mycan.cplatform.CplatformApplication.main(CplatformApplication.java:12) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_202]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_202]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_202]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_202]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.6.4.jar:2.6.4]
Caused by: java.lang.NullPointerException: null
    at springfox.documentation.spring.web.WebMvcPatternsRequestConditionWrapper.getPatterns(WebMvcPatternsRequestConditionWrapper.java:56) ~[springfox-spring-webmvc-3.0.0.jar:3.0.0]
    at springfox.documentation.RequestHandler.sortedPaths(RequestHandler.java:113) ~[springfox-core-3.0.0.jar:3.0.0]
    at springfox.documentation.spi.service.contexts.Orderings.lambda$byPatternsCondition$3(Orderings.java:89) ~[springfox-spi-3.0.0.jar:3.0.0]
    at java.util.Comparator.lambda$comparing$77a9974f$1(Comparator.java:469) ~[na:1.8.0_202]
    at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) ~[na:1.8.0_202]
    at java.util.TimSort.sort(TimSort.java:220) ~[na:1.8.0_202]
    at java.util.Arrays.sort(Arrays.java:1512) ~[na:1.8.0_202]
    at java.util.ArrayList.sort(ArrayList.java:1462) ~[na:1.8.0_202]
    at java.util.stream.SortedOps$RefSortingSink.end(SortedOps.java:387) ~[na:1.8.0_202]
    at java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:1.8.0_202]
    at java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:1.8.0_202]
    at java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:1.8.0_202]
    at java.util.stream.Sink$ChainedReference.end(Sink.java:258) ~[na:1.8.0_202]
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482) ~[na:1.8.0_202]
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_202]
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_202]
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_202]
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_202]
    at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:81) ~[springfox-spring-webmvc-3.0.0.jar:3.0.0]
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_202]
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382) ~[na:1.8.0_202]
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_202]
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_202]
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_202]
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_202]
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_202]
    at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.withDefaults(AbstractDocumentationPluginsBootstrapper.java:107) ~[springfox-spring-web-3.0.0.jar:3.0.0]
    at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.buildContext(AbstractDocumentationPluginsBootstrapper.java:91) ~[springfox-spring-web-3.0.0.jar:3.0.0]
    at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.bootstrapDocumentationPlugins(AbstractDocumentationPluginsBootstrapper.java:82) ~[springfox-spring-web-3.0.0.jar:3.0.0]
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:100) ~[springfox-spring-web-3.0.0.jar:3.0.0]
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-5.3.16.jar:5.3.16]
    ... 19 common frames omitted

二、問題排查

查看異常堆棧的頂部,發(fā)現(xiàn)是此處報空指針異常:springfox.documentation.spring.web.WebMvcPatternsRequestConditionWrapper#getPatterns,

跟蹤condition變量,發(fā)現(xiàn)是從springfox.documentation.spring.web.WebMvcRequestHandler#getPatternsCondition方法返回的WebMvcPatternsRequestConditionWrapper類的對象返回的。

找到類org.springframework.web.servlet.mvc.method.RequestMappingInfo,發(fā)現(xiàn)有兩個很相近的方法,查看方法注釋可以知道當(dāng)其中一個起作用時,另外一個則返回null。

所以,springfox的代碼似乎不應(yīng)該這樣寫,而是應(yīng)該加上一個判斷。

實際上,SpringBoot 2.6.0開始,請求路徑與Spring MVC處理映射匹配的默認策略已從AntPathMatcher更改為PathPatternParser。

但是可以通過設(shè)置spring.mvc.pathmatch.matching-strategy為ant-path-matcher來改變。

所以我們在application.properties文件中加上一行配置:

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

啟動項目,發(fā)現(xiàn)還是報錯。

繼續(xù)看,發(fā)現(xiàn)還是有HandlerMethod的patternsCondition為空。

那么為了不報空指針異常,只能把這些HandlerMethod過濾掉,這些HandlerMethod是從RequestMappingInfoHandlerMapping獲取的,那么通過過濾RequestMappingInfoHandlerMapping就可以達到目標(biāo),通過判斷條件getPatternParser() == null進行判斷,這樣過濾之后,所有的HandlerMethod的getPatternsCondition()都不是為空的了。

于是,我們對WebMvcRequestHandlerProvider類型的Bean進行改造,添加代碼:

@Bean
public 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);
                }
            }
        };
    }

三、解決方案        

1. 添加配置文件:

spring.mvc.pathmatch.matching-strategy=ant_path_matcher     

2. 添加代碼配置:

@Bean
public 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);
                }
            }
        };
    }

四、其他        

Spring fox 已經(jīng)大半年沒有更新了,為了使用swagger折騰的費勁,其實現(xiàn)在也有其他的api文檔方案,比如Spring doc和Apifox,可以考慮更換。

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中將String類型依照某個字符分割成數(shù)組的方法

    Java中將String類型依照某個字符分割成數(shù)組的方法

    下面小編就為大家分享一篇Java中將String類型依照某個字符分割成數(shù)組的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • MybatisPlus 插入或更新數(shù)據(jù)時自動填充更新數(shù)據(jù)解決方案

    MybatisPlus 插入或更新數(shù)據(jù)時自動填充更新數(shù)據(jù)解決方案

    本文主要介紹了MybatisPlus 插入或更新數(shù)據(jù)時自動填充更新數(shù)據(jù)解決方案,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • java中ThreadLocal和ThreadLocalMap淺析

    java中ThreadLocal和ThreadLocalMap淺析

    這篇文章主要介紹了java中ThreadLocal和ThreadLocalMap淺析,ThreadLocal類用來設(shè)置線程私有變量?本身不儲存值?主要提供自身引用?和?操作ThreadLocalMap?屬性值得方法,需要的朋友可以參考下
    2023-09-09
  • IDEA去除xml文件的黃色背景的操作步驟

    IDEA去除xml文件的黃色背景的操作步驟

    這篇文章主要介紹了IDEA去除xml文件的黃色背景的方法,本文通過圖文結(jié)合的方式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考價值,感興趣的朋友可以參考下
    2023-12-12
  • java實現(xiàn)背單詞程序

    java實現(xiàn)背單詞程序

    這篇文章主要為大家詳細介紹了java實現(xiàn)背單詞程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • 使用Spirng Boot Admin監(jiān)控Spring Cloud應(yīng)用項目

    使用Spirng Boot Admin監(jiān)控Spring Cloud應(yīng)用項目

    這篇文章主要介紹了使用Spirng Boot Admin監(jiān)控Spring Cloud應(yīng)用項目,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Java實現(xiàn)簡單雙色球搖獎功能過程解析

    Java實現(xiàn)簡單雙色球搖獎功能過程解析

    這篇文章主要介紹了Java實現(xiàn)簡單雙色球搖獎功能過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-09-09
  • idea運行java的配置詳細教程(包含maven,mysql下載配置)

    idea運行java的配置詳細教程(包含maven,mysql下載配置)

    程序員們在開發(fā)的時候,一定會用到Intellij?IDEA這個集成開發(fā)環(huán)境,這篇文章主要給大家介紹了關(guān)于idea運行java的配置(包含maven,mysql下載配置)的相關(guān)資料,需要的朋友可以參考下
    2024-05-05
  • 詳解Java線程-守護線程與用戶線程

    詳解Java線程-守護線程與用戶線程

    這篇文章主要介紹了Java守護線程與用戶線程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 創(chuàng)建Jersey REST 服務(wù),基于Maven的實現(xiàn)

    創(chuàng)建Jersey REST 服務(wù),基于Maven的實現(xiàn)

    下面小編就為大家?guī)硪黄獎?chuàng)建Jersey REST 服務(wù),基于Maven的實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06

最新評論

蒙城县| 屯昌县| 醴陵市| 竹山县| 英德市| 蓬安县| 秀山| 株洲市| 宁河县| 当阳市| 万年县| 潍坊市| 忻州市| 平南县| 宁武县| 兰州市| 大名县| 通辽市| 仙居县| 安塞县| 西峡县| 沾化县| 崇礼县| 忻州市| 闽侯县| 松潘县| 安顺市| 定结县| 麻阳| 正安县| 常德市| 日土县| 霍山县| 阿坝县| 楚雄市| 宁陵县| 荔浦县| 万源市| 平果县| 东海县| 沾益县|