天天看點

更新 SpringBoot 2.6.x 版本後,Swagger 沒法用了

作者:java網際網路架構
最近想體驗下最新版本的SpringBoot,逛了下官網,發現SpringBoot目前最新版本已經是2.6.4了,版本更新确實夠快的。之前的項目更新了2.6.4版本後發現有好多坑,不僅有循環依賴的問題,連Swagger都沒法用了!今天給大家分享下更新過程,填一填這些坑!

聊聊SpringBoot版本

首先我們來聊聊SpringBoot的版本,目前最新版本是2.6.4版本,2.7.x即将釋出,2.4.x及以下版本已經停止維護了,目前的主流版本應該是2.5.x和2.6.x。具體可以看下面這張表。

更新 SpringBoot 2.6.x 版本後,Swagger 沒法用了

更新過程

下面我們将之前的mall-tiny-swagger項目更新下,看看到底有哪些坑,這些坑該如何解決!

添加依賴

首先在pom.xml中修改SpringBoot的版本号,注意從2.4.x版本開始,SpringBoot就不再使用.RELEASE字尾了。

更新 SpringBoot 2.6.x 版本後,Swagger 沒法用了

循環依賴

  • 啟動項目後,由于SpringBoot禁止了循環引用,我們會遇到第一個問題,securityConfig和umsAdminServiceImpl循環引用了,具體日志如下;
更新 SpringBoot 2.6.x 版本後,Swagger 沒法用了

具體來說就是我們的SecurityConfig引用了UmsAdminService;

更新 SpringBoot 2.6.x 版本後,Swagger 沒法用了

而UmsAdminServiceImpl又引用了PasswordEncoder;

更新 SpringBoot 2.6.x 版本後,Swagger 沒法用了

由于SecurityConfig繼承了WebSecurityConfigurerAdapter,而Adapter又引用了PasswordEncoder,這樣就導緻了循環引用。

更新 SpringBoot 2.6.x 版本後,Swagger 沒法用了

要解決這個問題其實很簡單,你可以修改application.yml直接允許循環引用,不過這個方法有點粗暴,在沒有其他方法的時候可以使用;

更新 SpringBoot 2.6.x 版本後,Swagger 沒法用了

其實循環引用主要是因為會導緻Spring不知道該先建立哪個Bean才會被禁用的,我們可以使用@Lazy注解指定某個Bean進行懶加載就可以優雅解決該問題,比如在SecurityConfig中懶加載UmsAdminService。

更新 SpringBoot 2.6.x 版本後,Swagger 沒法用了

啟動出錯

  • 再次啟動SpringBoot應用後會出現一個空指針異常,一看就是Swagger問題,原來挺好用的Swagger不能用了!
更新 SpringBoot 2.6.x 版本後,Swagger 沒法用了

在Swagger的配置類中添加如下Bean可以解決該問題;

/**
 * Swagger2API文檔的配置
 */
@Configuration
public class Swagger2Config {

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

}           

文檔無法顯示

  • 再次啟動後通路Swagger文檔,會發現之前好好的文檔也無法顯示了,通路位址:http://localhost:8088/swagger-ui/
更新 SpringBoot 2.6.x 版本後,Swagger 沒法用了

修改application.yml檔案,MVC預設的路徑比對政策為PATH_PATTERN_PARSER,需要修改為ANT_PATH_MATCHER;

更新 SpringBoot 2.6.x 版本後,Swagger 沒法用了

再次啟動後發現Swagger已經可以正常使用了!

更新 SpringBoot 2.6.x 版本後,Swagger 沒法用了

聊聊springfox

提到Swagger,我們一般在SpringBoot中內建的都是springfox給我們提供的工具庫,看了下官網,該項目已經快兩年沒有釋出新版本了。

更新 SpringBoot 2.6.x 版本後,Swagger 沒法用了

再看下Maven倉庫中的版本,依舊停留在之前的3.0.0版本。如果springfox再不出新版本的話,估計随着SpringBoot版本的更新,相容性會越來越差的!

更新 SpringBoot 2.6.x 版本後,Swagger 沒法用了

總結

今天帶大家體驗了一把SpringBoot更新2.6.x版本的過程,主要解決了循環依賴和Swagger無法使用的問題,希望對大家有所幫助!