Springing Forward: A Smooth Transition to Spring Boot 3
Javed Khan
Posted on March 11, 2024
Introduction
In this article, we will delve into the essential steps and challenges involved in upgrading an existing application from Spring Boot 2 and Java 8 to Java 17 and Spring Boot 3. We will explore the solutions we implemented to overcome these issues, offering insights into potential stumbling blocks that may arise during your migration process.
Step 1: JDK 17 Installation
Begin the upgrade process by ensuring your system is equipped with Java Development Kit (JDK) 17. This version is the minimum requirement for Spring Boot 3 compatibility.
Step 2: Gradle Upgrade
Align your Gradle version with the prerequisites of Spring Boot 3. Upgrade to Gradle 7.5 or 8.x to optimize integration and performance.
Step 3: Build Configuration Updates
In your build.gradle file, update the build configuration to match the requirements of Spring Boot 3:
id 'org.springframework.boot' version '3.1.7'
id 'io.spring.dependency-management' version '1.1.4'
sourceCompatibility = '17'
Overcoming Challenges
- Package Name Changes from ‘javax’ to ‘jakarta’
Spring Boot 3 has embraced Jakarta EE, necessitating updates to package names. Here are the key changes:
javax.persistence.* → jakarta.persistence.*
javax.validation.* → jakarta.validation.*
javax.servlet.* → jakarta.servlet.*
javax.annotation.* → jakarta.annotation.*
javax.transaction.* → jakarta.transaction.*
java.mail.* → jakarta.mail.*
Note: Packages like javax.sql.* and javax.crypto.* remain unchanged, as they are part of Java 17 JDK.
- Handling Spring Security
In Spring Security version 5.7.0-M2, the WebSecurityConfigurerAdapter class underwent changes. Deprecated elements like authorizeRequests() and antMatchers() were replaced with authorizeHttpRequests() and requestMatchers().
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeHttpRequests()
.requestMatchers("/api/v1/auth/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authenticationProvider(authenticationProvider)
.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}
Refer to Spring Security Documentation for more details.
- Mail Service Dependency Update
Replace the deprecated javax.mail dependency with the Spring Boot provided dependency:
implementation 'org.springframework.boot:spring-boot-starter-mail'
- JPA Specification and Database Query
Address the generic error involving JPA Specification with a solution provided in this Stack Overflow thread.
- Handling Duplicate Key Errors
If encountering duplicate key errors after calling an API, adjust your entity class annotations from @GeneratedValue(strategy = GenerationStrategy.AUTO) to @GeneratedValue(strategy = GenerationType.SEQUENCE) and include @SequenceGenerator for each entity.
Hibernate 6.0 now creates a sequence per entity hierarchy.
- Deprecated Hibernate Annotation
If using @Type(type = “org.hibernate.type.PostgresUUIDType”), remove it as it is no longer required in Hibernate 6.0.
Conclusion
Navigating the upgrade to Spring Boot 3 might present challenges, yet addressing these issues ensures that your application remains up-to-date and takes full advantage of the latest features. Sharing our experiences fosters a collective understanding of potential hurdles in the migration process. We encourage you to share your challenges and solutions in the comments section below.
Posted on March 11, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.