springboot3-upgrade

Upgrading Springboot2 to Springboot3 - notes

Important:: Bckup your project!

  1. Update Java version to 17 in pom.xml

    1
    2
    3
    <properties>
    <java.version>17</java.version>
    </properties>
  2. Upgrade Spring Boot version to 3.x.x in pom.xml

    1
    2
    3
    4
    5
    6
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.0</version> <!-- Replace with the latest Spring Boot 3.x version -->
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
  3. Update dependencies in pom.xml

    As Spring Boot manages many dependencies, we need to go though each dependency we overrode for Spring Boot with the correct version. Usually the dependencies are: spring-boot-starter-web, spring-boot-starter-security, etc.

  4. Handle namespace changes

    The major changes in Spring Boot 3 is the migration from java.* to jakarta.*

  5. Update Spring Security configuration

    Replace WebSecurityConfigurerAdapter

    Before (Spring Boot 2):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    http
    .authorizeRequests()
    .anyRequest().authenticated()
    .and()
    .oauth2ResourceServer()
    .jwt();
    }
    }

    After (Spring Boot 3 with Spring Security 6):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    import org.springframework.context.annotation.Bean;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.web.SecurityFilterChain;

    @EnableWebSecurity
    public class SecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http
    .authorizeHttpRequests(authz -> authz
    .anyRequest().authenticated()
    )
    .oauth2ResourceServer(oauth2 -> oauth2
    .jwt()
    );
    return http.build();
    }
    }

    Also the test cases would be impacted, which are supposed to be updated as well.

    Before:

    1
    2
    3
    4
    5
    @RunWith(SpringRunner.class)
    @WebMvcTest(MyController.class)
    public class MyControllerTest {
    // ...
    }

    After:

    1
    2
    3
    4
    5
    6
    7
    8
    import org.junit.jupiter.api.extension.ExtendWith;
    import org.springframework.test.context.junit.jupiter.SpringExtension;

    @ExtendWith(SpringExtension.class)
    @WebMvcTest(MyController.class)
    public class MyControllerTest {
    // ...
    }
  6. In Spring Boot 3, the framework introduces Ahead-of-Time (AOT) compilation as part of its support for GraalVM Native Image generation. However, if you don’t need AOT compilation for your project or are facing issues related to it, you can easily disable it.

    • Disable in property file

      1
      spring.aot.enabled=false
    • Disable AOT in pom.xml

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      <build>
      <plugins>
      <!-- Spring Boot Maven Plugin -->
      <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
      <aot>false</aot> <!-- Disable AOT -->
      </configuration>
      </plugin>
      </plugins>
      </build>
    • Disable AOT for testing in pom.xml

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      <build>
      <plugins>
      <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <executions>
      <execution>
      <id>test</id>
      <goals>
      <goal>repackage</goal>
      </goals>
      <configuration>
      <aot>false</aot>
      </configuration>
      </execution>
      </executions>
      </plugin>
      </plugins>
      </build>
    • @DisabledInAotMode for the test cases with @MockBean


springboot3-upgrade
http://codingogo.github.com/2024/10/05/springboot3-upgrade/
Author
codingogo
Posted on
October 5, 2024
Licensed under