Spring: @Valid triggers Bean Validation on a request body

Without @Valid the constraints on your DTO are ignored and invalid data reaches your service.

Code
record OrderRequest(
        @NotBlank String sku,
        @Min(1) int qty,
        @Email String contact) { }

@PostMapping("/orders")
Order create(@Valid @RequestBody OrderRequest body) { ... }
Output
POST with qty = 0 returns:
400 Bad Request
{"errors":[{"field":"qty","message":"must be greater than or equal to 1"}]}
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11