Backend Development 7 min read

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

This article explains how to import validation dependencies in Spring Boot, compares the semantics of @NotNull, @NotBlank, and @NotEmpty annotations, demonstrates their usage with code examples, and clarifies the behavioral differences between @Valid and @Validated when validating controller inputs.

Top Architect
Top Architect
Top Architect
Understanding @NotNull, @NotBlank, @NotEmpty and the Difference Between @Valid and @Validated in Spring Boot

The article begins by showing how to add the Spring Boot web starter dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.0.5.RELEASE</version>
</dependency>

The import statement for the validation constraints is also provided:

import javax.validation.constraints.*;

It then describes three common validation annotations:

@NotNull

Applicable to basic data types (Integer, Long, Double, etc.). When used on a String , the value must not be null but may be empty.

Note: Fields annotated with @NotNull can also be constrained with @Size, @Max, @Min, etc.

@NotBlank

Applicable only to String values; the string must be non‑null and, after trim() , have a length greater than zero.

@NotEmpty

Applicable to String , collections, maps, arrays, etc.; the value must be non‑null and have a size greater than zero.

Example entity demonstrating these annotations:

@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 import statements for the validation groups are:

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

Two controller methods illustrate the use of @Valid and @Validated respectively:

@ApiOperation(value = "新增或者修改一个人的信息")
@PostMapping("/updateOrInsert")
public Result updateOrInsert(@Valid @RequestBody Person person) {
    Boolean updateOrInsert = personService.updateOrInsert(person);
    if (updateOrInsert) {
        return new Result(ResultCode.SUCCESS, updateOrInsert);
    }
    return new Result(ResultCode.ERROR, "新增或者修改一个人的信息失败");
}

@ApiOperation(value = "新增或者修改一个人的信息")
@PostMapping("/updateOrInsert")
public Result updateOrInsert(@Validated @RequestBody Person person) {
    Boolean updateOrInsert = personService.updateOrInsert(person);
    if (updateOrInsert) {
        return new Result(ResultCode.SUCCESS, updateOrInsert);
    }
    return new Result(ResultCode.ERROR, "新增或者修改一个人的信息失败");
}

Key differences highlighted:

Both @Valid and @Validated trigger validation of fields annotated with constraints such as @NotNull, @NotEmpty, etc.

When using @Valid , a BindingResult must be provided to capture validation errors; if not handled, execution continues.

When using @Validated , validation failures throw a 400 error automatically, stopping method execution; a global exception handler is needed to return custom messages.

Overall, @Validated offers a more concise approach, reducing boilerplate code.

The article concludes with a brief mention of other common validation annotations available in the same package.

ValidationAnnotationsSpringBoot@Validated@ValidNotBlankNotEmptyNotNull
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn 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.