Spring Boot 升级到2.7的踩坑总结
这篇文章分享一下Spring Boot 升级到2.7的踩坑总结还是挺全面的希望对大家有所帮助~说明2.7.2为2.x的最后一个稳定版本。3开始最低要求 Java 17所以暂时不到3.x。以下的处理方法主要针对我们的项目可能并不通用。1、hibernate-validator包下的类报错Springboot从2.3以后spring-boot-starter-web中不再引入hibernate-validator需要手动引入。在父pom中引入已经加入software-center-modules模块中子模块不需要加dependency groupIdorg.hibernate.validator/groupId artifactIdhibernate-validator/artifactId version6.0.18.Final/version scopecompile/scope /dependency2、ErrorController无getErrorPath方法去掉该方法3、logback和log4j冲突org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j at org.apache.logging.slf4j.Log4jLoggerFactory.validateContext(Log4jLoggerFactory.java:60) at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:44) at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:33) at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:53) at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:33) at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:363) at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:388) at com.ld.CreditTaskManageApplication.clinit(CreditTaskManageApplication.java:40) ... 34 more排除掉springboot的loggingdependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-batch/artifactId exclusions !-- spring boot 默认的日志框架是Logback,所以在引用log4j之前需要先排除该包的依赖再引入log4j2的依赖 -- exclusion groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-logging/artifactId /exclusion /exclusions /dependency4、循环依赖The dependencies of some of the beans in the application context form a cycle[credit-task-manage]2022-08-04 13:54:43.411 [WARN]:Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name webMainConfig: Unsatisfied dependency expressed through field handlerAdapter; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration: Unsatisfied dependency expressed through method setConfigurers parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name webMainConfig: Requested bean is currently in creation: Is there an unresolvable circular reference? org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:591) The dependencies of some of the beans in the application context form a cycle: ┌─────┐ | webMainConfig (field private org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter com.ld.common.config.WebMainConfig.handlerAdapter) ↑ ↓ | org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration └─────┘ 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. org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter.report(LoggingFailureAnalysisReporter.java:40)WebMainConfig类中 去掉报错的方法和属性handlerAdapter修改写法Bean public ConversionService getConversionService(DateConverter dateConverter) { ConversionServiceFactoryBean factoryBean new ConversionServiceFactoryBean(); SetConverterString, Date converters new HashSet(); converters.add(dateConverter); factoryBean.setConverters(converters); return factoryBean.getObject(); }日期转换器依赖hutool的日期工具类处理如不满足再自行扩展import java.util.Date; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.converter.Converter; import cn.hutool.core.date.DateUtil; /** * Description 表单形式的全局时间类型转换器 */ Configuration public class DateConverter implements ConverterString, Date { // 可以根据前端传递的时间格式自动匹配格式化 Override public Date convert(String source) { return DateUtil.parse(source); } }代码中应该尽量避免循环依赖的情况如果出现了加Lazy注解懒加载。代码中不鼓励依赖循环引用默认情况下禁止使用循环引用。如果能消除bean之间的依赖循环最好消除如果实在改动太大还有一种不推荐的处理方法设置 spring.main.allow-circular-referencestrue5、swagger错误Failed to start bean documentationPluginsBootstrapperApplication run failed org.springframework.boot.SpringApplication.reportFailure(SpringApplication.java:824) 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.22.jar:5.3.22] 复制代码启动报了“Failed to start bean documentationPluginsBootstrapper”再往下面看到“springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider”就可以断定是跟Swagger相关的问题。查资料发现是新版本Spring Boot将Spring MVC默认路径匹配策略由AntPathMatcher更改为PathPatternParser因此我们可以通过配置让其仍使用AntPathMatcher即可。关注公号码猿技术专栏回复关键词1111获取阿里内部调优手册解决方案在application.properties里配置# 路径匹配策略使用旧版本的 spring.mvc.pathmatch.matching-strategy ANT_PATH_MATCHER顺便升级swagger到swagger3已经加到base公共包里了5.1、修改后路径需要修改默认首页由swagger-ui.html变成了/swagger-ui/index.html5.2、如果还想使用扩展的2个ui的版本也需要跟着升级swagger-ui-layer.version1.1.3/swagger-ui-layer.version swagger-bootstrap-ui.version1.9.6/swagger-bootstrap-ui.version我这里直接删除了那2个ui使用了swagger-bootstrap-ui的升级版knife4j。base模块中已经引入knife4j.version3.0.3/knife4j.version …… dependency groupIdcom.github.xiaoymin/groupId artifactIdknife4j-spring-boot-starter/artifactId version${knife4j.version}/version /dependency5.3、swagger的配置类注解EnableSwagger2去掉名字改为更通用的SwaggerConfigConfiguration //EnableSwagger2 Slf4j public class SwaggerConfig { }5.4、删除项目中自定义的pringfox.documentation.spring.web.readers包5.5、去掉自定义的页面如果想修改找到新的jar包复制出页面进行调整否则可能看到的页面里没有内容src/main/resources/META-INF/resources/doc.html5.6 调整过滤器路径配置# 安全访问配置SecurityFilter # 需要过滤的urlPatterns多个用^分隔没有或为空则不限制 security.access.urlPatterns /doc.html^/docs.html^/swagger-ui.html^/swagger-ui/index.html^/v2/api-docs^/swagger-resources6、跳转登录页出错如果出现跳转时出错Cannot forward to error page for request [/a/] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false org.springframework.boot.web.servlet.support.ErrorPageFilter.handleCommittedResponse(ErrorPageFilter.java:219)解决方案同57、日期转换出错升级后发现java中是Date类型数据库中datetime类型Timestamp类型没有问题的数据不是转换为Timestamp而是直接转为LocalDateTime类型了解决办法com.ld.shieldsb.dao.MyBeanProcessor修改type2Bean方法增加LocalDateTime和LocalDate的处理if (value ! null fieldType.equals(Date.class)) { if (value.getClass().equals(String.class)) { SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd HH:mm:ss); try { field.set(model, sdf.parse((String) value)); } catch (ParseException e) { log.error(日期字段读取失败, fieldType ,error: e.getMessage()); } } else if (value.getClass().equals(LocalDateTime.class)) { field.set(model, DateTimeUtil.localDateTime2Date((LocalDateTime) value)); } else if (value.getClass().equals(LocalDate.class)) { field.set(model, DateTimeUtil.localDate2Date((LocalDate) value)); } else { field.set(model, value); } }我们使用的是mysql查看依赖jar包看到mysql-connector-java的版本从8.0.19变成了8.0.29原因找到com.mysql.cj.jdbc.result.ResultSetImpl类的getObject(int columnIndex)方法可以看到Datetime类型的确实换了类型case DATETIME: return getTimestamp(columnIndex); // 改为了 case DATETIME: return getLocalDateTime(columnIndex);8、flywayorg.flywaydb.core.api.FlywayException: Unsupported Database: MySQL 5.7Application run failed org.springframework.boot.SpringApplication.reportFailure(SpringApplication.java:824) org.springframework.beans.factory.BeanCreationException: Error creating bean with name flywayInitializer defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Unsupported Database: MySQL 5.7 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.22.jar:5.3.22] …… at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:343) [bootstrap.jar:9.0.31] at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:474) [bootstrap.jar:9.0.31] Caused by: org.flywaydb.core.api.FlywayException: Unsupported Database: MySQL 5.7 at org.flywaydb.core.internal.database.DatabaseTypeRegister.getDatabaseTypeForConnection(DatabaseTypeRegister.java:106) ~[flyway-core-8.5.13.jar:?] ……org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.22.jar:5.3.22] ... 49 moreflyway对数据库版本有要求例如flyway-core的当前版本V8.4.3不能使用 MySQL 5.7 当flyway-core 降低到V7.15.0后 问题解决所以匹配flyway-core和数据库版本后问题即可解决。properties …… flyway.version7.15.0/flyway.version /properties …… !-- 添加 flyway 的依赖,flyway需要区分版本不同版本对不同数据库版本支持不同 -- dependency groupIdorg.flywaydb/groupId artifactIdflyway-core/artifactId version${flyway.version}/version /dependency ……9、Junit运行后没有反应升级后默认使用junit5而依赖的jar包中引入了junit4的jar包冲突了去掉junit4的jar包即可。注意使用junit5后包的名字发生了变化下面箭头前后分别是junit4和junit5的org.junit.Test》org.junit.jupiter.api.Test org.junit.runner.RunWith》org.junit.jupiter.api.extension.ExtendWith //使用时 RunWith(SpringRunner.class)》ExtendWith(SpringExtension.class)10、升级后json中Long类型字段精度丢失出现如下情况前面是真实值后面为json传递后的值344280995828072448》344280995828072450 344268472663932928》344268472663932900 343301120241696768》343301120241696800原项目中是有Long转字符串的处理的。问题原因经查看默认已经有多个消息转换器了。而 configureMessageConverters 方法中是一个 list 参数。直接向其中添加 HttpMessageConverter 后默认是排在最后的。就造成了你自定义的消息转换器不生效。其实是被其他转换器接管了。解决办法加到第一个就行了。add(0, customConverter())Override public void configureMessageConverters(ListHttpMessageConverter? converters) { FastJsonHttpMessageConverter fastConverter new FastJsonHttpMessageConverter(); …… // 支持text 转string converters.add(0, customJackson2HttpMessageConverter()); converters.add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8)); converters.add(0, fastConverter); }如果使用的是bean注解覆盖的fastjson则不需要改如下Bean public HttpMessageConverters customConverters() { FastJsonHttpMessageConverter fastConverter new FastJsonHttpMessageConverter(); // 创建配置类 FastJsonConfig fastJsonConfig new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty); // 解决 Long 转json 精度丢失的问题 SerializeConfig serializeConfig SerializeConfig.globalInstance; serializeConfig.put(BigInteger.class, ToStringSerializer.instance); serializeConfig.put(Long.class, ToStringSerializer.instance); serializeConfig.put(Long.TYPE, ToStringSerializer.instance); fastJsonConfig.setSerializeConfig(serializeConfig); // 此处是全局处理方式 fastJsonConfig.setDateFormat(DATE_FORMAT); fastJsonConfig.setCharset(StandardCharsets.UTF_8); fastConverter.setFastJsonConfig(fastJsonConfig); ListMediaType supportedMediaTypes new ArrayList(); supportedMediaTypes.add(MediaType.APPLICATION_JSON); fastConverter.setSupportedMediaTypes(supportedMediaTypes); // 支持text 转string StringHttpMessageConverter stringHttpMessageConverter new StringHttpMessageConverter(); return new HttpMessageConverters(fastConverter, stringHttpMessageConverter); }

相关新闻

Python爬虫实战:架构的韧性 - 基于 Schema Versioning 的爬虫字段演化与动态适配机制!

Python爬虫实战:架构的韧性 - 基于 Schema Versioning 的爬虫字段演化与动态适配机制!

㊗️本期内容已收录至专栏《Python爬虫实战》,持续完善知识体系与项目实战,建议先订阅收藏,后续查阅更方便~ ㊙️本期爬虫难度指数:⭐⭐⭐ 🉐福利: 一次订阅后,专栏内的所有文章可永…

2026/7/3 16:38:43 阅读更多 →
“低代码适配、小时级切换、业务平稳过渡”——金仓数据库如何支撑Oracle国产化替换的规模化落地?

“低代码适配、小时级切换、业务平稳过渡”——金仓数据库如何支撑Oracle国产化替换的规模化落地?

“低代码适配、小时级切换、业务平稳过渡”——金仓数据库如何支撑Oracle国产化替换的规模化落地? 导语(152字) 某省级政务平台曾因Oracle年均维保支出超280万元、关键补丁升级导致审批系统单次中断47分钟,被迫启动为期六个月的去…

2026/7/3 12:14:18 阅读更多 →
小白程序员转行必看:AI大模型训练师如何抓住未来机遇?

小白程序员转行必看:AI大模型训练师如何抓住未来机遇?

马斯克预测AI将在2026年超越人类个体,2030年超越人类总和,预示着AI技术将深刻改变行业和职场。AI大模型训练师作为需求旺盛、门槛适中的新兴岗位,为不同背景的职场人提供了进入AI领域的绝佳机会。通过数据标注、指令优化等工作,帮…

2026/7/4 4:11:43 阅读更多 →

最新新闻

大模型API商用成本拆解:Token计价、上下文溢价与企业级隐性费用

大模型API商用成本拆解:Token计价、上下文溢价与企业级隐性费用

1. 这份价格表不是“查价工具”,而是商用决策的导航仪你手头正跑着一个客户定制的智能客服项目,月底要签二期合同;或者刚在内部立项了AI辅助写周报的SaaS功能,技术方案定了,但财务部卡在成本测算环节;又或者…

2026/7/4 10:44:21 阅读更多 →
AI就绪笔记本采购指南:硬件选型与代码大模型落地实战

AI就绪笔记本采购指南:硬件选型与代码大模型落地实战

1. 项目概述:这不是一份普通早报,而是一份面向技术决策者与硬件从业者的“信号解码器”“通讯Plus早报|24年笔记本电脑出货量或超1亿 信通院公布AI代码大模型评估”——这个标题里藏着两股真实涌动的产业暗流。它不是媒体通稿的简单搬运&…

2026/7/4 10:44:21 阅读更多 →
YOLOv8中GAM注意力机制的实现与优化

YOLOv8中GAM注意力机制的实现与优化

1. GAM注意力机制的技术背景与核心价值 在目标检测领域,YOLOv8作为当前最先进的实时检测框架,其性能提升一直备受关注。传统卷积神经网络在处理特征图时存在一个根本性局限:所有空间位置和通道维度都被平等对待,而实际上不同区域和…

2026/7/4 10:40:19 阅读更多 →
基于YOLOv8的红外光伏板缺陷检测系统设计与实现

基于YOLOv8的红外光伏板缺陷检测系统设计与实现

1. 项目概述:基于YOLOv8的红外光伏板缺陷检测系统光伏板作为清洁能源的核心组件,其表面缺陷会直接影响发电效率。传统人工检测方式效率低下且容易漏检,我们团队开发的这套系统采用YOLOv8目标检测算法,实现了对光伏板缺陷的自动化识…

2026/7/4 10:40:19 阅读更多 →
从AI小白到高效协作者:普通人快速上手的实战指南

从AI小白到高效协作者:普通人快速上手的实战指南

1. 项目概述:为什么“ALL IN AI”不再是口号最近和不少朋友聊天,发现一个挺有意思的现象:前两年大家聊起AI,还觉得是硅谷大厂和顶尖实验室的“神仙打架”,离自己很远。但今年,从写周报、做PPT,到…

2026/7/4 10:38:18 阅读更多 →
13DOF传感器与MKV46F128VLH16微控制器的嵌入式导航方案

13DOF传感器与MKV46F128VLH16微控制器的嵌入式导航方案

1. 13DOF传感器与MKV46F128VLH16微控制器的技术背景在嵌入式定位导航领域,13DOF(13自由度)传感器组合与MKV46F128VLH16微控制器的搭配已经成为工业级应用的黄金组合。13DOF通常由三轴加速度计、三轴陀螺仪、三轴磁力计、气压计和温度传感器组…

2026/7/4 10:36:18 阅读更多 →

日新闻

Memcached 1.6.43 发布:关键安全修复版本,多项问题得到解决

Memcached 1.6.43 发布:关键安全修复版本,多项问题得到解决

Memcached 1.6.43 正式发布,这是一个关键的安全修复版本,修复了多个方面的问题,还对部分功能进行了优化。 安全修复亮点 此次发布在安全修复上表现突出。binprot 避免了项目引用计数溢出,mcmc 因安全问题提升了上游版本号&#xf…

2026/7/4 0:04:29 阅读更多 →
终极指南:使用HMCL启动器跨平台畅玩Minecraft的完整解决方案

终极指南:使用HMCL启动器跨平台畅玩Minecraft的完整解决方案

终极指南:使用HMCL启动器跨平台畅玩Minecraft的完整解决方案 【免费下载链接】HMCL A Minecraft Launcher which is multi-functional, cross-platform and popular 项目地址: https://gitcode.com/gh_mirrors/hm/HMCL HMCL(Hello Minecraft! Lau…

2026/7/4 0:06:29 阅读更多 →
KMX63与PIC18F66K40在嵌入式HMI中的硬件协同与低功耗设计

KMX63与PIC18F66K40在嵌入式HMI中的硬件协同与低功耗设计

1. KMX63与PIC18F66K40的硬件协同架构解析KMX63作为一款三轴加速度计和磁力计组合传感器,与PIC18F66K40微控制器的搭配堪称嵌入式HMI开发的黄金组合。这套硬件组合的核心优势在于KMX63提供的高精度运动感知能力与PIC18F66K40强大的信号处理能力形成了完美互补。KMX6…

2026/7/4 0:06:29 阅读更多 →

周新闻

月新闻