Example of Spring Bean Validation – JSR-303 Annotations
Step 1: Set up a new Spring MVC project
- Create a new Maven project in your preferred IDE (e.g., IntelliJ or Eclipse or Spring Tool Suite) and add the following dependencies.
- Spring Web
- Lombok
- spring-boot-starter-validation
XML configuration in pom.xml file as shown below:
XML
<? xml version = "1.0" encoding = "UTF-8" ?> < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0</ modelVersion > < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >3.2.0</ version > < relativePath /> <!-- lookup parent from repository --> </ parent > < groupId >com.spring</ groupId > < artifactId >Spring_JSR</ artifactId > < version >0.0.1-SNAPSHOT</ version > < name >Spring_JSR</ name > < description >Demo project for Spring_JSR</ description > < properties > < java.version >17</ java.version > </ properties > < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-validation</ artifactId > </ dependency > < dependency > < groupId >org.projectlombok</ groupId > < artifactId >lombok</ artifactId > < optional >true</ optional > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-test</ artifactId > < scope >test</ scope > </ dependency > </ dependencies > < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > < configuration > < excludes > < exclude > < groupId >org.projectlombok</ groupId > < artifactId >lombok</ artifactId > </ exclude > </ excludes > </ configuration > </ plugin > </ plugins > </ build > </ project > |
Project Structure:
Step 2: Create a Pojo class with JSR Annotations [Student]
Java
package com.demo.model; import jakarta.validation.constraints.AssertTrue; import jakarta.validation.constraints.Digits; import jakarta.validation.constraints.Email; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Pattern; import jakarta.validation.constraints.Size; import lombok.Data; import org.hibernate.validator.constraints.URL; @Data public class Student { @NotNull @Size (min = 3 , max = 50 , message = "Name must be between three to fifty characters" ) private String name; @Email (message = "Invalid email format" ) private String email; @NotNull (message = "Age cannot be null" ) @Min (value = 18 , message = "Age must be greater than or equal to eighteen" ) private Integer age; @NotEmpty (message = "Address cannot be empty" ) private String address; @Pattern (regexp = "\\d{10}" , message = "Phone number must be ten digits" ) private String phoneNumber; public Student( @NotNull @Size (min = 3 , max = 50 , message = "Name must be between three to fifty characters" ) String name, @Email (message = "Invalid email format" ) String email, @NotNull (message = "Age cannot be null" ) @Min (value = 18 , message = "Age must be greater than or equal to eighteen" ) Integer age, @NotEmpty (message = "Address cannot be empty" ) String address, @Pattern (regexp = "\\d{10}" , message = "Phone number must be 10 digits" ) String phoneNumber) { super (); this .name = name; this .email = email; this .age = age; this .address = address; this .phoneNumber = phoneNumber; } } |
Step 3: Create a Controller that handles all validations [StudentController]
Java
package com.demo.controller; import com.demo.model.Student; import jakarta.validation.Valid; import java.util.List; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class StudentController { @PostMapping ( "/student" ) public String createUser( @RequestBody @Valid Student student, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return "Validation errors: " + bindingResult.getAllErrors(); } return "Student created successfully: " + student.toString(); } } |
Step 4: Run the Application
- Now you can run the spring boot application from IDE or command line.
mvn spring-boot:run
- Visit http://localhost:8080/students in tools like curl, Postman,, and you should see the courses page.
Here , we are using POSTMAN for Testing the application.
Output 1: Age is less than 18
Output 2: Name is NotNull and Size of name is between 3 and 50 characters
Output 3: Email is Incorrect
Output 4: If the all fields are in correct format and passed the all requirement
Spring Bean Validation – JSR-303 Annotations
In this article, we’ll explore practical examples of how to apply JSR-303 annotations to your domain objects from basic annotations to advanced. So basically annotations provide a declarative way to configure Spring beans, manage dependencies, and define behaviors, reducing the need for boilerplate code and making your code more concise and expressive.
Contact Us