关于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…

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理