Backend Development 14 min read

Spring Boot 2.7 Upgrade Pitfalls and Solutions

This article provides a comprehensive guide to upgrading Spring Boot to version 2.7, detailing common issues such as missing dependencies, logging conflicts, circular bean references, Swagger configuration changes, Flyway compatibility, JUnit version mismatches, and JSON Long precision loss, along with concrete code‑based solutions.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Spring Boot 2.7 Upgrade Pitfalls and Solutions

The author shares a thorough summary of the pitfalls encountered when upgrading a Spring Boot project to version 2.7, aiming to help developers avoid common errors.

Prerequisites: 2.7.2 is the last stable 2.x release and requires Java 17; the following fixes are specific to the author's project and may need adaptation.

1. Hibernate‑Validator missing: Starting with Spring Boot 2.3, <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.0.18.Final</version> <scope>compile</scope> </dependency> must be added manually in the parent POM.

2. ErrorController getErrorPath removed: Simply delete the getErrorPath() method from the custom ErrorController .

3. Logback vs Log4j conflict: Exclude Spring Boot's default Logback logging and add Log4j2 explicitly: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> .

4. Circular bean dependency: Remove the problematic method and handlerAdapter from WebMainConfig , and define a conversion service bean to avoid the cycle: @Bean public ConversionService getConversionService(DateConverter dateConverter) { ConversionServiceFactoryBean factoryBean = new ConversionServiceFactoryBean(); Set > converters = new HashSet<>(); converters.add(dateConverter); factoryBean.setConverters(converters); return factoryBean.getObject(); } . A global date converter can be implemented as: @Configuration public class DateConverter implements Converter { @Override public Date convert(String source) { return DateUtil.parse(source); } } . If a cycle is unavoidable, enable it with spring.main.allow-circular-references=true or annotate beans with @Lazy .

5. Swagger bean start failure: Spring Boot 2.6+ switches the path‑matching strategy to PathPatternParser , causing Swagger to fail. Restore the old matcher: spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER . Upgrade Swagger to version 3 or switch to Knife4j: add <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency> and rename the config class to SwaggerConfig (remove @EnableSwagger2 ).

6. Login page forward error: The same configuration change in step 5 resolves the “Cannot forward to error page” issue.

7. Date conversion to LocalDateTime: Adjust MyBeanProcessor to handle LocalDateTime and LocalDate types, e.g.: 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)); } .

8. Flyway unsupported MySQL 5.7: Flyway 8.x does not support MySQL 5.7. Downgrade Flyway to 7.15.0 (or another compatible version) in the pom.xml : <flyway.version>7.15.0</flyway.version> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> <version>${flyway.version}</version> </dependency> .

9. JUnit5 vs JUnit4 conflict: After the upgrade, JUnit 5 is used by default. Remove JUnit 4 dependencies and replace annotations: org.junit.Test → org.junit.jupiter.api.Test , org.junit.runner.RunWith → org.junit.jupiter.api.extension.ExtendWith , e.g., @RunWith(SpringRunner.class) → @ExtendWith(SpringExtension.class) .

10. Long precision loss in JSON: The custom FastJsonHttpMessageConverter must be placed at the beginning of the converters list and configured to serialize Long and BigInteger as strings: converters.add(0, fastConverter); converters.add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8)); converters.add(0, customJackson2HttpMessageConverter()); 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); .

The article concludes with a request for likes, follows, and shares, and provides links to PDF collections of related Spring Cloud, Spring Boot, and MyBatis tutorials.

backendJavatestingMavenSpring BootUpgradeSwaggerFlyway
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.