Backend Development 6 min read

Understanding @NotNull, @NotBlank, @NotEmpty, @Valid and @Validated in Spring Boot

This article explains the purpose, differences, and proper usage of Spring validation annotations such as @NotNull, @NotBlank, @NotEmpty, as well as the @Valid and @Validated triggers, providing Maven dependency setup and concrete code examples for effective backend data validation.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Understanding @NotNull, @NotBlank, @NotEmpty, @Valid and @Validated in Spring Boot

The article begins by showing how to add the Spring Boot starter web dependency in <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.0.5.RELEASE</version> </dependency> and notes that the validation annotations reside in import javax.validation.constraints.*; .

It then distinguishes three common constraint annotations:

@NotNull – applicable to primitive wrapper types and also to String ; the field cannot be null but may be empty.

@NotBlank – only for String ; the value must be non‑null and, after trim() , have a length greater than zero.

@NotEmpty – works on String , collections, maps, arrays, etc.; the value cannot be null nor have a size of zero.

Example entity code demonstrates these annotations together with Swagger annotations and length checks:

@Data
public class BigPeople {
    @ApiModelProperty(value = "id", required = true)
    @NotNull(message = "id不能为空")
    @Length(message = "id不能超过{max}个长度", max = 10)
    private Integer id;

    @ApiModelProperty(value = "name", required = true)
    @NotBlank(message = "name不能为空")
    @Size(message = "名字最长为{max} 个字", max = 10)
    private String name;

    @ApiModelProperty(value = "age", required = true)
    @NotNull(message = "age不能为空")
    @Range(message = "age的长度范围为{min}岁到{max}岁之间", min = 5, max = 10)
    private Integer age;

    @ApiModelProperty(value = "treeNode", required = true)
    @NotEmpty(message = "treeNode不能为空")
    private List<String> treeNode;
}

The article also shows where to import the validation trigger annotations:

import javax.validation.Valid;
import org.springframework.validation.annotation.Validated;

Two controller method examples illustrate the difference between using @Valid and @Validated on a request body:

@ApiOperation(value = "新增或者修改一个人的信息")
@PostMapping("/updateOrInsert")
public Result updateOrInsert(@Valid @RequestBody Person person) { ... }

@ApiOperation(value = "新增或者修改一个人的信息")
@PostMapping("/updateOrInsert")
public Result updateOrInsert(@Validated @RequestBody Person person) { ... }

Key points of comparison:

Both annotations activate the constraint annotations on fields.

@Valid requires a BindingResult to capture validation errors; if not handled, execution continues.

@Validated throws a 400 error automatically on failure, stopping method execution; a global exception handler is needed to return custom messages.

Overall, @Validated leads to cleaner controller code.

Finally, the article mentions that the same package contains many other useful validation annotations, encouraging readers to explore them for robust backend data checks.

backend developmentSpring BootannotationsHibernate ValidatorJava Validation
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

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.