Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

With only a short while until the release of Spring Boot 3, now seems like a good time to check out what’s new.

Further reading:

Java 14 Record Keyword

Explore the fundamentals of records, including their purpose, generated methods, and customization techniques.

Sealed Classes and Interfaces in Java

Explore sealed classes and interfaces, a preview feature in Java SE 15, delivered in Java 17.

Native Images with Spring Boot and GraalVM

Learn how to use Spring Native to compile and build native images using Buildpacks and GraalVM's native build tools

2. Java 17

While there was already support for Java 17 before, this LTS version now gets the baseline.

When migrating from LTS version 11, Java developers will benefit from new language features. Since Java itself isn’t the topic of this article, we’ll only name the most important new features for Spring Boot developers. We can find additional details in the separate articles for Java 17, 16, 15, 14, 13, and 12.

2.1. Records

Java records (JEP 395, see Java 14 Record Keyword) were intended to be used as a quick way to create data carrier classes, i.e. the classes whose objective is to simply contain data and carry it between modules, also known as POJOs (Plain Old Java Objects) and DTOs (Data Transfer Objects).

We can easily create immutable DTOs:

public record Person (String name, String address) {}

Currently, we need to be careful when combining them with Bean Validation because validation constraints aren’t supported on constructor arguments, such as when the instance is created on JSON deserialization (Jackson) and put into a controller’s method as a parameter.

2.2. Text Blocks

With JEP 378, it’s now possible to create multi-line text blocks without the need to concatenate strings on line breaks:

String textBlock = """
Hello, this is a
multi-line
text block.
""";

2.3. Switch Expressions

Java 12 introduced switch expressions (JEP 361), which (like all expressions) evaluate a single value, and can be used in statements. Instead of combining nested ifelse-operators (?:), we can now use a switchcase-construct:

DayOfWeek day = DayOfWeek.FRIDAY;
int numOfLetters = switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> 6;
    case TUESDAY                -> 7;
    case THURSDAY, SATURDAY     -> 8;
    case WEDNESDAY              -> 9;
};

2.4. Pattern Matching

Pattern Matchings were elaborated in Project Amber and found their way to the Java Language. In the Java Language, they can help to simplify the code for instanceof evaluations.

We can use them directly with instanceof:

if (obj instanceof String s) {
    System.out.println(s.toLowerCase());
}

We can also use it within a switchcase statement:

static double getDoubleUsingSwitch(Object o) {
    return switch (o) {
        case Integer i -> i.doubleValue();
        case Float f -> f.doubleValue();
        case String s -> Double.parseDouble(s);
        default -> 0d;
    };
}

2.5. Sealed Classes and Interfaces

Sealed classes can limit inheritance by specifying allowed subclasses:

public abstract sealed class Pet permits Dog, Cat {}

We can find more details in Sealed Classes and Interfaces in Java.

3. Jakarta EE 9

The most important change might be the jump from Java EE to Jakarta EE9, where the package namespace changed from javax.* to jakarta.*. As a result, we need to adjust all imports in our code whenever we use classes from Java EE directly.

For example, when we access the HttpServletRequest object within our Spring MVC Controller, we need to replace:

import javax.servlet.http.HttpServletRequest;

with:

import jakarta.servlet.http.HttpServletRequest;

Of course, we don’t have to use the types of the Servlet API very often, but this is unavoidable if we use bean validation and JPA.

We should also be aware of this when we use external libraries that depend on Java/Jakarta EE (e.g. we have to use Hibernate Validator 7+, Tomcat 10+, and Jetty 11+).

4. Further Dependencies

Spring Framework 6 and Spring Boot 3 need the following minimum versions:

5. Big Points

Two overarching topics have received particular attention: Native Executables and Observability. Overarching means that:

  • the Spring Framework introduces core abstractions
  • the portfolio projects consistently integrate with them
  • Spring Boot provides auto-configuration

5.1. Native Executables

Building native executables and deploying them to GraalVM gets a higher priority. So the Spring Native initiative is moving into Spring proper.

For AOT generation, there’s no need to include separate plugins, we can just use a new goal of the spring-boot-maven-plugin:

mvn spring-boot:aot-generate

Native Hints will also be part of the Spring core. Testing infrastructure for this will be available with Milestone 5 (v6.0.0-M5).

5.2. Observability

Spring 6 introduces Spring Observability – a new initiative that builds on Micrometer and Micrometer Tracing (formerly Spring Cloud Sleuth). The goal is to efficiently record application metrics with Micrometer and implement tracing through providers, such as OpenZipkin or OpenTelemetry.

There’s auto-configuration for all of these in Spring Boot 3, and the Spring projects are working on instrumenting themselves using the new Observation API.

We can find more details about it in the dedicated article.

6. Smaller Changes in Spring Web MVC

One of the most important new features is the support for RFC7807 (Problem Details Standard). Now we won’t need to include separate libraries, like Zalando Problem.

Another smaller change is that HttpMethod is no longer an enum, but a class that allows us to create instances for extended HTTP methods, e.g. those defined by WebDAV :

HttpMethod lock = HttpMethod.valueOf("LOCK");

At least some outdated servlet-based integrations are dropped, like Commons FileUpload (we should use StandardServletMultipartResolver  for multipart file uploads), Tiles, and FreeMarker JSP support (we should use FreeMarker template views instead).

7. Migrating Projects

There are a few hints for project migration that we should know. The recommended steps are:

  1. Migrate to Spring Boot 2.7 (when Spring Boot 3 is released, there will be a migration guide based on Spring Boot 2.7)
  2. Check for deprecated code usage and legacy config file processing; it will be removed with the new major release
  3. Migrate to Java 17
  4. Check third-party projects to have Jakarta EE 9 compatible releases
  5. Since Spring Boot 3 isn’t yet released, we can try the current milestone to test the migration

8. Conclusion

As we’ve learned, migrating to Spring Boot 3 and Spring 6 will be a migration to Java 17 and Jakarta EE 9 too. If we attach great importance to observability and native executables, we’ll benefit the most from the upcoming major release.

As always, all the code is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.