Discover the Top 5 New Features in Spring Boot 4 Preview
This article introduces Spring Boot 4.0.0 preview, built on Spring Framework 7, and walks through five major enhancements—including elegant API versioning, flexible bean registration, null‑safety with JSpecify, simplified HTTP proxy creation, plus several other upgrades and deprecations—complete with code samples and screenshots.
When creating a new Spring Boot project, the preview version Spring Boot 4.0.0 is already available.
Spring Boot 4 is built on the brand‑new Spring Framework 7.0.0.
Here are the most important changes in the new version.
1. Elegant version control
The new version introduces graceful API versioning support, allowing developers to specify a version parameter in the @RequestMapping annotation.
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class VersionedController {
@RequestMapping(value = "/user", version = "1")
public String getUserV1() {
// version 1 implementation
System.out.println("Version 1");
return "Version 1";
}
@RequestMapping(value = "/user", version = "2")
public String getUserV2() {
// version 2 implementation
System.out.println("Version 2");
return "Version 2";
}
}Execution result:
2. Convenient bean injection
The new version adds a BeanRegistrar contract that enables flexible registration of multiple beans at once.
import org.springframework.beans.factory.BeanRegistrar;
import org.springframework.beans.factory.BeanRegistry;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
@Configuration
@Import(MyBeansRegistrar.class)
public class MyConfiguration {
}
class MyBeansRegistrar implements BeanRegistrar {
@Override
public void register(BeanRegistry registry, Environment env) {
registry.registerBean("user", User.class);
if (env.matchesProfiles("dev")) {
registry.registerBean(Order.class, spec -> spec.supplier(context -> new Order("order_001")));
}
}
}
class User {
private String name;
}
class Order {
public Order(String name) { this.name = name; }
private String name;
}A unit test proves the registered beans work correctly:
3. Null‑safety improvements
The new version adopts JSpecify annotations to declare API nullability, using @Nullable for optional values and @NonNull for required ones, with IDE warnings.
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
public class Person {
private String name;
public void setName(@NonNull String name) {
this.name = name;
}
@Nullable
public String getName() {
return this.name;
}
}IDE shows the warning as shown:
Different IDE versions display the warning differently; the author used IntelliJ IDEA 2024.
4. Easy HTTP proxy creation
The new @ImportHttpServices annotation makes creating HTTP service proxies simpler, allowing you to declare, detect, and configure whole groups of HTTP services.
@Configuration(proxyBeanMethods = false)
@ImportHttpServices(group = "weather", types = {FreeWeather.class, CommercialWeather.class})
public class HttpServicesConfiguration extends AbstractHttpServiceRegistrar {
@Bean
public RestClientHttpServiceGroupConfigurer groupConfigurer() {
return groups -> groups.filterByName("weather", "user")
.configureClient((group, builder) -> builder.defaultHeader("User-Agent", "My-Application"));
}
}5. Other seven changes
Additional upgrades include:
SPEL expression upgrade : support for null‑safe and Elvis operators, e.g., @Value("#{systemProperties['pop3.port'] ?: 25}").
GraalVM native application support : compile applications to native images via Spring AOT, drastically reducing startup time.
Jackson 3.x support : drop support for Jackson 2.x and upgrade to Jackson 3.x.
Servlet and WebSocket version upgrade : use Servlet 6.1 and WebSocket 2.2, requiring Tomcat 11+ or Jetty 12.1+.
HttpHeaders optimization : new firstValue method and easier iteration of headers.
Feature removals : deprecated Spring MVC XML namespace, JUnit 4 support in TestContext, Jackson 2.x, and Spring JCL.
Higher minimum environment requirements : Jakarta EE 11, Kotlin 2.x, JSONassert 2.0, GraalVM 23.
For more upgrade details, see the official release notes: https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-7.0-Release-Notes
Conclusion
Framework and tool changes bring more convenient and friendly usage for developers, making it a win for programmers to learn, use, and experiment.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
