What’s new in Spring Boot 3.0
I know it has been a while since the Spring Boot 3.0 update on 24 November. However, there are a few features worth mentioning:
- Jakarta EE
- GraalVM Native Images
- Spring Data 2022
- HTTP Interfaces
- Observability
- Problem Detail
- Java 17 (quickly)
Jakarta EE
Jakarta EE is a set of specifications. The specification is an umbrella term for a set of interfaces, abstract classes, types, etc. You can think of it as a library.
I’ve included a few examples below, and you can find a full list of specifications here.
- DB connection (previously javax.persistance, now jakarta.persistance)
- Network communication (jakarta.servlet)
- JSON Processing
Example of imports in Spring 2.x and 3.x
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
vs.
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
In my opinion, this is a much more cosmetic change than a functional one. Of course, the project and dependencies will be cleaner.
GraalVM Native Images
GraalVM is basically another JDK distribution whose main advantages are:
- High-performance
- AOT - Ahead-of-time compilation (bypass interpretation, precompiling only required classes)
- Peak performance without warmup time
- Multi-language support (Python, JS, Java, R…)
Native Java means a program uses AOT compilation to produce an executable image. Such an image can run standalone, offering quick startup, low latency, and low CPU/memory requirements.
However, you need to be aware of the restrictions and compatibility guide:
- Unreachable during image creation code will be removed
- No lazy loading
- Fixed classpath
- @Profile annotation is not supported
This feature can be very useful for deploying FaaS (Function as a Service) in the cloud. See if it fits into your application.
Spring Data
Main updates in Spring Data area:
- CRUD repository interfaces that return lists instead of iterators. You don’t need additional mapping anymore.
- Consistent deletion method behavior across modules.
deleteById
— should not throw an exception if no entity was deleteddelete
— should throw OptimisticLockingException exception if nothing was deleted and the entity has a version property. Otherwise should not throw an exception. - Sorting repositories does not inherit from CRUD repositories as it was before
- Removal of support Joda Time, ThreeTenBackport, RxJava 1 and 2
HTTP Interfaces
Spring Boot 3 allows you to define HTTP interfaces as Java interfaces. Such interfaces have HTTP exchange methods. You can define a proxy that implements this interface to perform these exchanges. This feature requires a WebFlux dependency in your project.
In a nutshell interface declaration.
interface ResourceService {
@GetExchange("/resources/{resourceId}/")
Resource getResource(@PathVariable String resourceId);
}
@GetExchange
can be replaced by any other HTTP method.
And a proxy implementation.
WebClient client = WebClient.builder().baseUrl("https://resources.com").build();
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();
ResourcesService service = factory.createClient(ResourcesService.class);
So we have another way to communicate with an external API. Compared to e.g. RestTemplate, it may look cleaner at first glance. However, there is still a proxy to configure.
Observability
Observability consists of logging, tracing and metrics.
The metrics and tracing are the responsibility of Micrometer, which is built into Spring Boot 3. You don’t need a third-party library like before.
In addition, the Micrometer Observation API allows you to define exactly which parts of your code should be traced.
Problem Detail
Have you ever implemented a custom error response object, used Spring Error Attributes, or even an external library like Zalando Problem?
Spring Boot 3 introduces a new object calledProblemDetail
which is the implementation of RFC-7807. As described in the above document, it consists of the following parameters:
- type — URI redirection to the webpage which describes a problem type in human-readable format.
- title — a quick human-readable summary of what happened
- status — http status
- detail — explanation of this particular problem
- instance — URI of this particular request
Java 17
The default Java version from now on will be Java 17. There are already a bunch of articles describing the new features, so don’t blame me, but I’m going to move on.
Other Links
- https://jakarta.ee/
- https://www.graalvm.org/
- https://spring.io/blog/2022/11/24/spring-boot-3-0-goes-ga
- https://www.youtube.com/watch?v=TR254zh-f3c
- https://github.com/spring-projects/spring-data-commons/wiki/Spring-Data-2022.0-%28Turing%29-Release-Notes
- https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html
- https://docs.spring.io/spring-framework/docs/current/reference/html/integration.html#rest-http-interface
- https://docs.spring.io/spring-boot/docs/current/api//org/springframework/boot/web/servlet/error/DefaultErrorAttributes.html
- https://www.rfc-editor.org/rfc/rfc7807