AI Hints
Home›Spring Boot Tutorial›Why Validate, Bean Validation and @Valid
Spring Boot · Lesson 75 of 120

@Valid

Check the request body.

⏱ 2 min read▶ Video at 1:40:59🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

The rules are written. But nothing checks them yet. At Valid tells Spring, check this object, before calling my method.

  • Rules are written
  • Nothing checks them yet
  • @Valid

Turning validation on

The record now has three rules. At Valid goes before at Request Body. Now Spring checks the book, before our method runs. Run it. A good book is saved. The junk is rejected with four hundred, and our method never runs.

BookController.javajava
@RestController
public class BookController {

    @PostMapping("/books")
    public String add(@Valid @RequestBody NewBook book) {
        return "Saved: " + book;
    }
}
Output
$ curl --json '{"title":"Dune","author":"Herbert","price":9.99}' localhost:8080/books
Saved: NewBook[title=Dune, author=Herbert, price=9.99]
$ curl -i --json '{"title":"","author":"","price":-5}' localhost:8080/books
HTTP/1.1 400
Content-Type: application/json

{
  "timestamp":"2026-11-25T16:27:31.880Z",
  "status":400,
  "error":"Bad Request",
  "path":"/books"
}
Also uses NewBook.java
public record NewBook(
    @NotBlank String title,
    @NotBlank String author,
    @Positive double price
) {}
Also uses _curl.sh
curl --json '{"title":"Dune","author":"Herbert","price":9.99}' localhost:8080/books
curl -i --json '{"title":"","author":"","price":-5}' localhost:8080/books

A problem left

The four hundred is right. But look at the body. It doesn't say which fields were wrong, or why. We'll fix that in the next lesson, with our own error handler.

keyvalue
Status400, correct
Bodyno field details
Clientcan't tell what to fix
Nextan error handler

Path and query values

Constraints also work on path variables, and request parameters, with no extra setup. Id must be at least one. And size must be at most fifty. Run it. Book three is fine. Id zero is rejected, with four hundred. And size five hundred is rejected too.

BookController.javajava
@RestController
public class BookController {

    @GetMapping("/books/{id}")
    public String one(@PathVariable @Min(1) int id) {
        return "Book " + id;
    }

    @GetMapping("/books")
    public String page(@RequestParam(defaultValue = "10") @Max(50) int size) {
        return "Page size " + size;
    }
}
Output
$ curl localhost:8080/books/3
Book 3
$ curl -i localhost:8080/books/0 | head -1
HTTP/1.1 400
$ curl -i "localhost:8080/books?size=500" | head -1
HTTP/1.1 400
Also uses _curl.sh
curl localhost:8080/books/3
curl -i localhost:8080/books/0 | head -1
curl -i "localhost:8080/books?size=500" | head -1

Summary

At Valid checks a request body against its rules. Constraints on path variables, and parameters, are checked automatically. Next, clear validation errors.

Key takeaways
  • @Valid @RequestBody
  • @Min on parameters
  • 400 on bad input
Next up: Validation Errors — tell the client what is wrong