0
0
SpringbootHow-ToBeginner · 4 min read

How to Use @NotBlank in Spring Boot for Input Validation

In Spring Boot, use the @NotBlank annotation on string fields in your request DTOs to ensure they are neither null nor empty after trimming. Combine it with @Valid in your controller method parameters to trigger validation automatically.
📐

Syntax

The @NotBlank annotation is placed above a string field in a class to enforce that the field must contain at least one non-whitespace character. It is part of the javax.validation.constraints package.

Use it like this:

  • @NotBlank(message = "Field cannot be blank") - custom error message
  • Place it on string fields in DTOs or entities
  • Trigger validation by annotating controller method parameters with @Valid
java
import javax.validation.constraints.NotBlank;

public class UserRequest {
    @NotBlank(message = "Username must not be blank")
    private String username;

    // getters and setters
}
💻

Example

This example shows a Spring Boot REST controller that validates a request body with a @NotBlank field. If the field is blank or missing, Spring returns a 400 Bad Request with the validation error.

java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@RestController
class UserController {
    @PostMapping("/users")
    public ResponseEntity<String> createUser(@Valid @RequestBody UserRequest request) {
        return ResponseEntity.ok("User created: " + request.getUsername());
    }
}

class UserRequest {
    @NotBlank(message = "Username must not be blank")
    private String username;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}
Output
POST /users with body {"username":""} returns 400 Bad Request with error: "Username must not be blank" POST /users with body {"username":"alice"} returns 200 OK with body: "User created: alice"
⚠️

Common Pitfalls

  • Using @NotBlank on non-string fields will not work as expected; it only applies to strings.
  • For null checks on any type, use @NotNull instead.
  • For strings that can be empty but not null, use @NotNull combined with other annotations.
  • For validation to trigger, the controller method parameter must be annotated with @Valid and the request body must be parsed correctly.
java
/* Wrong usage: @NotBlank on Integer field - will not validate properly */
class WrongRequest {
    @NotBlank
    private Integer age; // Incorrect
}

/* Correct usage: @NotNull for non-string fields */
import javax.validation.constraints.NotNull;
class CorrectRequest {
    @NotNull
    private Integer age; // Correct
}
📊

Quick Reference

Summary tips for using @NotBlank in Spring Boot:

  • Use @NotBlank only on string fields to ensure they are not null, empty, or whitespace.
  • Always annotate controller method parameters with @Valid to enable validation.
  • Customize error messages with the message attribute.
  • Combine with other validation annotations for complex rules.

Key Takeaways

Use @NotBlank on string fields to ensure they are not null, empty, or whitespace only.
Annotate controller method parameters with @Valid to trigger validation automatically.
@NotBlank is only for strings; use @NotNull for other types.
Customize validation messages with the message attribute for better user feedback.
Validation errors result in 400 Bad Request responses in Spring Boot REST APIs.