Loading...
41

I'm building a REST API and need to validate the request body. I have a UserDTO class:

__CODEBLOCK0__

How do I validate that email is not null and is a valid email format, name is not blank, and age is between 18 and 100?

Also, how do I return proper error messages when validation fails?

asked 5 days ago

Log in to add a comment.

1 Answer

35

Here's how to implement validation in Spring Boot:

Step 1: Add dependency
__CODEBLOCK0

*Step 2: Annotate your DTO
_CODE_BLOCK_1_

Step 3: Use @Valid in controller
_CODE_BLOCK_2_

Step 4: Handle validation errors*
CODEBLOCK3__

answered 4 days ago
boot_dev 850
Log in to answer this question.
Question Stats
Asked: 5 days ago
Views: 1.1k
Active: 1 day ago
Related Tags