Spring / Spring Boot 4 Basics Interview Questions
What are JSpecify nullability annotations in Spring Boot 4 and why are they important?
Spring Boot 4 adopts JSpecify annotations (@Nullable, @NonNull, @NullMarked) portfolio-wide, deprecating Spring's own org.springframework.lang annotations. JSpecify is a collaborative standard backed by Google, JetBrains, Broadcom (Spring), Uber, Oracle, and OpenJDK.
| Annotation | Package | Meaning |
|---|---|---|
| @NonNull | org.jspecify.annotations | The element is never null; tooling enforces this contract |
| @Nullable | org.jspecify.annotations | The element may be null; callers must handle null |
| @NullMarked | org.jspecify.annotations | Applied at package/class level: all unannotated types are non-null by default |
| @NullUnmarked | org.jspecify.annotations | Opts a scope back out of null-marked assumptions |
// package-info.java: declare the whole package as null-safe @NullMarked package com.example.service; import org.jspecify.annotations.NullMarked; // Service class: unannotated types are non-null by default @Service public class OrderService { // Non-null by default (from @NullMarked) public Order createOrder(String productId, int quantity) { return new Order(productId, quantity); } // Explicitly mark where null is possible: public @Nullable Order findOrder(@Nullable String orderId) { if (orderId == null) return null; return orderRepository.findById(orderId).orElse(null); } } // Maven dependency: <dependency> <groupId>org.jspecify</groupId> <artifactId>jspecify</artifactId> <!-- Version managed by Spring Boot 4 BOM --> </dependency> // Kotlin benefit: JSpecify annotations are automatically // translated into Kotlin nullability by Kotlin 2.2 compiler // val order: Order? = service.findOrder(null) <- correct type // val order: Order = service.createOrder("p1", 2) <- non-null inferred
Tool support: IntelliJ IDEA 2025.3 ships first-class JSpecify support with full data-flow analysis. Build-time null checking via NullAway (requires JDK 21+) catches nullability violations at compile time. Previously fragmented null annotation libraries (Spring's own, JetBrains', FindBugs) are being consolidated around JSpecify.
More Related questions...