Enterprise Intermediate

Spring 7 adopts JSpecify annotations, making non-null the default and reducing annotation noise.

โœ• Spring 5/6
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;

public class UserService {

    @Nullable
    public User findById(@NonNull String id) {
        return repository.findById(id).orElse(null);
    }

    @NonNull
    public List<User> findAll() {
        return repository.findAll();
    }

    @NonNull
    public User save(@NonNull User user) {
        return repository.save(user);
    }
}
โœ“ Spring 7
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
public class UserService {

    public @Nullable User findById(String id) {
        return repository.findById(id).orElse(null);
    }

    public List<User> findAll() {
        return repository.findAll();
    }

    public User save(User user) {
        return repository.save(user);
    }
}
See a problem with this code? Let us know.
โœ‚๏ธ

Non-null by default

@NullMarked makes all unannotated types non-null, so only nullable exceptions need annotation.

๐ŸŒ

Ecosystem standard

JSpecify annotations are a cross-framework standard recognized by NullAway, Error Prone, and IDEs.

๐Ÿ”

Richer tooling

Modern static analyzers understand JSpecify's null model and report violations at compile time.

Old Approach
Spring @NonNull/@Nullable
Modern Approach
JSpecify @NullMarked
Since JDK
17
Difficulty
Intermediate
Spring Null Safety with JSpecify
Available

Available since Spring Framework 7.0 (requires Java 17+)

Spring 5 and 6 introduced their own null safety annotations in the `org.springframework.lang` package. While useful, these were framework-specific and required annotating every non-null element explicitly. Spring 7 migrates to JSpecify, a cross-ecosystem standard for null safety. The `@NullMarked` annotation at the class or package level declares that all unannotated types are non-null by default. Only actual nullable types need the `@Nullable` annotation, dramatically reducing verbosity. JSpecify annotations are recognized by major static analysis tools such as NullAway, Error Prone, and IntelliJ IDEA, bringing richer tooling support beyond what Spring-specific annotations provided.

Share ๐• ๐Ÿฆ‹ in โฌก