Backend Development 7 min read

Discover Spring Boot 4.0 Snapshot: New API Versioning, Virtual Threads & More

Spring Boot 4.0 (SNAPSHOT) introduces built‑in Spring Framework 7.0 support, elegant API versioning via @RequestMapping, flexible bean registration, virtual thread integration for massive concurrency, JSpecify null‑safety annotations, enhanced SPEL, Jackson 3.x migration, servlet upgrades, and native GraalVM image support, all illustrated with code samples.

macrozheng
macrozheng
macrozheng
Discover Spring Boot 4.0 Snapshot: New API Versioning, Virtual Threads & More

Elegant API Versioning

Spring Boot 4.0 allows you to add a

version

attribute directly to

@RequestMapping

, enabling multiple API versions to coexist on the same URL. You can also implement

WebMvcConfigurer

and override

configureApiVersioning

to define custom version‑resolution strategies, such as request parameters, headers, path variables, or even custom resolvers based on User‑Agent or IP.

<code>@RestController
@RequestMapping("/api")
public class VersionedController {

    @RequestMapping(value = "/user", version = "1")
    public String getUserV1() {
        return "Version 1 - 老铁,你好!";
    }

    @RequestMapping(value = "/user", version = "2")
    public String getUserV2() {
        return "Version 2 - Hi buddy, what's up!";
    }
}
</code>

Testing with curl shows the appropriate versioned response based on the

version

query parameter.

<code>curl "http://localhost:8080/?version=1" => "API Version 1.0.0"
curl "http://localhost:8080/?version=2" => "API Version 2.0.0"
</code>

Flexible Bean Injection Mechanism

By implementing the

BeanRegistrar

interface, you can programmatically register beans with custom logic, such as environment‑specific beans.

<code>@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(ctx -> new Order("order_dev_001")));
        }
    }
}
</code>

Virtual Thread Support

Spring Boot 4.0 leverages JDK 21 virtual threads, enabling million‑level concurrency. Enabling it is as simple as setting

spring.threads.virtual.enabled=true

. Existing

@Async

methods work seamlessly, and a new

/virtual-threads

actuator endpoint provides real‑time thread metrics.

Embracing JSpecify to Eliminate NPEs

Integration with JSpecify adds

@Nullable

and

@NonNull

annotations, allowing IDEs like IntelliJ IDEA 2024 to flag potential null‑pointer issues at compile time.

<code>public class Person {
    private String name;
    public void setName(@NonNull String name) { this.name = name; }
    @Nullable
    public String getName() { return this.name; }
}
</code>

Enhanced SPEL

The Spring Expression Language now supports null‑safe navigation and the Elvis operator (

?:

), simplifying expressions such as

#{systemProperties['pop3.port'] ?: 25}

.

Other Notable Changes

Full migration to Jackson 3.x – Jackson 2 is no longer supported.

Servlet API upgraded to 6.1 and WebSocket to 2.2.

Improved GraalVM native image support via Ahead‑of‑Time compilation.

API versioningJavaSPELSpring BootVirtual ThreadsJSpecify
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.