把项目的springboot版本从2.3.12.RELEASE升到2.6.2遇到的问题

2022-07-03 08:29:51

直接改了父项目,记录一下遇到的几个问题,2.6新版本的特性这篇文章讲的很好,有些也是参考的这里:Spring Boot 2.6 发布了和一些重要变更!!

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
    </parent>

先是遇到了这个问题

Description:

The dependencies of some of the beans in the application context form a cycle:

┌──->──┐
|  com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration
└──<-──┘


Action:

Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.

2.6版本默认禁止Spring Bean之间的循环引用,如果直接使用会报错,需要开启如下设置:

spring.main.allow-circular-references=true

但是其实PageHelper已经有新版本解决了,更新pagehelper版本到1.4.1就好了

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.1</version>
        </dependency>

另外还可能遇到这个问题

Description:

The following configuration properties have incompatible values: [spring.mvc.pathmatch.matching-strategy, spring.mvc.pathmatch.use-suffix-pattern]

Action:

Review the docs for spring.mvc.pathmatch.matching-strategy, spring.mvc.pathmatch.use-suffix-pattern and change the configured values.

2.6版本请求路径与 Spring MVC 处理映射匹配的默认策略已从AntPathMatcher更改为PathPatternParser。可以设置spring.mvc.pathmatch.matching-strategy为ant-path-matcher

spring:
  mvc:
    pathmatch:
      matching-strategy: ant-path-matcher #2.6版本Spring MVC 处理映射匹配的默认策略已从AntPathMatcher更改为PathPatternParser
      use-suffix-pattern: true

报 不兼容的类型: boolean无法转换为org.springframework.boot.web.error.ErrorAttributeOptions

应该是下面这个方法出问题了

getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.TEXT_HTML)));

改成下面这样就好了,好像是2.5版本就改了

getErrorAttributes(request, this.getErrorAttributeOptions(request, MediaType.TEXT_HTML)));

还有可能遇到的坑,2.6.2的getErrorAttributes方法默认不返回"message"参数,如果直接查这个可能会报空指针异常,网上查的方法说在直接yml配置文件中加下面这个就可以

server:
  error:
    include-exception: true
    include-message: always

但我试了不行,我就自己建了个配置类,然后在自己的全局异常处理类中引用这个配置类。内容我全复制的原来的ErrorProperties类,只改了构造方法里的this.includeMessage,NEVER改成ALWAYS就好了。改动完的类放在下面

public class ErrorProperties extends org.springframework.boot.autoconfigure.web.ErrorProperties{
    @Value("${error.path:/error}")
    private String path = "/error";
    private boolean includeException;
    private org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeAttribute includeStacktrace;
    private org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeAttribute includeMessage;
    private org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeAttribute includeBindingErrors;
    private final org.springframework.boot.autoconfigure.web.ErrorProperties.Whitelabel whitelabel;

    public ErrorProperties() {
        this.includeStacktrace = org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeAttribute.NEVER;
//改的下面这行
        this.includeMessage = org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeAttribute.ALWAYS;
        this.includeBindingErrors = org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeAttribute.NEVER;
        this.whitelabel = new org.springframework.boot.autoconfigure.web.ErrorProperties.Whitelabel();
    }

    public String getPath() {
        return this.path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public boolean isIncludeException() {
        return this.includeException;
    }

    public void setIncludeException(boolean includeException) {
        this.includeException = includeException;
    }

    public org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeAttribute getIncludeStacktrace() {
        return this.includeStacktrace;
    }

    public void setIncludeStacktrace(org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeAttribute includeStacktrace) {
        this.includeStacktrace = includeStacktrace;
    }

    public org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeAttribute getIncludeMessage() {
        return this.includeMessage;
    }

    public void setIncludeMessage(org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeAttribute includeMessage) {
        this.includeMessage = includeMessage;
    }

    public org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeAttribute getIncludeBindingErrors() {
        return this.includeBindingErrors;
    }

    public void setIncludeBindingErrors(org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeAttribute includeBindingErrors) {
        this.includeBindingErrors = includeBindingErrors;
    }

    public org.springframework.boot.autoconfigure.web.ErrorProperties.Whitelabel getWhitelabel() {
        return this.whitelabel;
    }

    public static class Whitelabel {
        private boolean enabled = true;

        public Whitelabel() {
        }

        public boolean isEnabled() {
            return this.enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }
    }

    public static enum IncludeAttribute {
        NEVER,
        ALWAYS,
        ON_PARAM;

        private IncludeAttribute() {
        }
    }

    public static enum IncludeStacktrace {
        NEVER,
        ALWAYS,
        ON_PARAM;

        private IncludeStacktrace() {
        }
    }
}

jackson core升到了最新版本2.13.1,这个应该是和spring版本对应,2.3.12.RELEASE可以用2.11.4

hutool只升到5.7.0,5.7.1开始CollUtil删除所有Map相关操作,因为用了newhashmap方法所以只能升到5.7.0

试运行了一下暂时没有遇到其他问题

与最新版本有关的信息可以在下面的网站查找

springboot     https://github.com/spring-projects/spring-boot/releases
pagehelper    https://github.com/pagehelper/pagehelper-spring-boot
jackson          https://github.com/FasterXML/jackson
hutool            https://www.hutool.cn/docs/#/CHANGELOG

  • 作者:风落拾羽
  • 原文链接:https://blog.csdn.net/qq_40444870/article/details/122383673
    更新时间:2022-07-03 08:29:51