乐趣区

关于java:升级-SpringBoot-26x-版本后Swagger-没法用了

最近想体验下最新版本的 SpringBoot,逛了下官网,发现 SpringBoot 目前最新版本曾经是 2.6.4 了,版本更新的确够快的。之前的我的项目降级了 2.6.4 版本后发现有好多坑,不仅有循环依赖的问题,连 Swagger 都没法用了!明天给大家分享下降级过程,填一填这些坑!

SpringBoot 实战电商我的项目 mall(50k+star)地址:https://github.com/macrozheng/mall

聊聊 SpringBoot 版本

首先咱们来聊聊 SpringBoot 的版本,目前最新版本是 2.6.4 版本,2.7.x行将公布,2.4.x及以下版本曾经进行保护了,目前的支流版本应该是 2.5.x2.6.x。具体能够看下上面这张表。

降级过程

上面咱们将之前的 mall-tiny-swagger 我的项目降级下,看看到底有哪些坑,这些坑该如何解决!

增加依赖

首先在 pom.xml 中批改 SpringBoot 的版本号,留神从 2.4.x 版本开始,SpringBoot 就不再应用 .RELEASE 后缀了。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.4</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

循环依赖

  • 启动我的项目后,因为 SpringBoot 禁止了 循环援用 ,咱们会遇到第一个问题,securityConfigumsAdminServiceImpl循环援用了,具体日志如下;

  • 具体来说就是咱们的 SecurityConfig 援用了UmsAdminService

  • UmsAdminServiceImpl 又援用了PasswordEncoder

  • 因为 SecurityConfig 继承了WebSecurityConfigurerAdapter,而 Adapter 又援用了PasswordEncoder,这样就导致了循环援用。

  • 要解决这个问题其实很简略,你能够批改 application.yml 间接容许循环援用,不过这个办法有点粗犷,在没有其余办法的时候能够应用;
spring:
  main:
    allow-circular-references: true
  • 其实循环援用次要是因为会导致 Spring 不晓得该先创立哪个 Bean 才会被禁用的,咱们能够应用 @Lazy 注解指定某个 Bean 进行懒加载就能够优雅解决该问题,比方在 SecurityConfig 中懒加载UmsAdminService

启动出错

  • 再次启动 SpringBoot 利用后会呈现一个空指针异样,一看就是 Swagger 问题,原来挺好用的 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…

  • 批改 application.yml 文件,MVC 默认的门路匹配策略为PATH_PATTERN_PARSER,须要批改为ANT_PATH_MATCHER
spring:
  mvc:
    pathmatch:
      matching-strategy: ANT_PATH_MATCHER
  • 再次启动后发现 Swagger 曾经能够失常应用了!

聊聊 springfox

提到 Swagger,咱们个别在 SpringBoot 中集成的都是 springfox 给咱们提供的工具库,看了下官网,该我的项目曾经快两年没有公布新版本了。

再看下 Maven 仓库中的版本,仍旧停留在之前的 3.0.0 版本。如果 springfox 再不出新版本的话,预计随着 SpringBoot 版本的更新,兼容性会越来越差的!

总结

明天带大家体验了一把 SpringBoot 降级 2.6.x 版本的过程,次要解决了循环依赖和 Swagger 无奈应用的问题,心愿对大家有所帮忙!

如果你想理解更多 SpringBoot 实战技巧的话,能够试试这个带全套教程的实战我的项目(50K+Star):https://github.com/macrozheng/mall

参考资料

官网地址:https://github.com/springfox/…

我的项目源码地址

https://github.com/macrozheng…

退出移动版