In this post, I am going to explain how to validate the data classes using Micronaut validator.
Step 1: Annotate the POJO with @Introspected annotation.
@Introspected
public class Employee {
@Positive
private int id;
@NotBlank
private String name;
@Min(18)
private int age;
.........
.........
}
Step 2: Validate employee object using Validator instance.
Validator validator = applicationContext.getBean(Validator.class);
Employee emp = new Employee();
emp.setName("");
emp.setId(-1);
emp.setAge(0);
Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(emp);
Find the below working application.
Step 1: Create new maven project ‘micronaut-manual-bean-validation’.
Step 2: Update pom.xml with maven dependencies.
pom.xml
<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>
<groupId>com.sample.app</groupId>
<artifactId>micronaut-manual-bean-validation</artifactId>
<version>1</version>
<properties>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<micronaut.version>3.7.3</micronaut.version>
<slf4j.version>2.0.3</slf4j.version>
<maven.compiler.target>15</maven.compiler.target>
<maven.compiler.source>15</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject-java</artifactId>
<version>${micronaut.version}</version>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-runtime</artifactId>
<version>${micronaut.version}</version>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-validation</artifactId>
<version>${micronaut.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.sample.app.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Step 3: Define Employee model class.
Employee.java
package com.sample.app.model;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Positive;
import io.micronaut.core.annotation.Introspected;
@Introspected
public class Employee {
@Positive
private int id;
@NotBlank
private String name;
@Min(18)
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
Step 4: Define main application class.
App.java
package com.sample.app;
import com.sample.app.model.Employee;
import io.micronaut.context.ApplicationContext;
import io.micronaut.validation.validator.Validator;
import java.util.*;
import javax.validation.ConstraintViolation;
public class App {
public static void main(String[] args) {
try (ApplicationContext applicationContext = ApplicationContext.run()) {
Validator validator = applicationContext.getBean(Validator.class);
Employee emp = new Employee();
System.out.println("Validating employee with invalid data");
emp.setName("");
emp.setId(-1);
emp.setAge(0);
Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(emp);
for (ConstraintViolation<Employee> constraintViolation : constraintViolations) {
System.out.println("\t" + constraintViolation.getPropertyPath() + " -> " + constraintViolation.getMessage());
}
emp.setAge(34);
emp.setId(1);
emp.setName("Krishna");
System.out.println("\nValidating employee with valid data");
constraintViolations = validator.validate(emp);
for (ConstraintViolation<Employee> constraintViolation : constraintViolations) {
System.out.println("\t" + constraintViolation.getPropertyPath() + " -> " + constraintViolation.getMessage());
}
System.out.println("emp : " + emp);
}
}
}
Total project structure looks like below.
Build the project using mvn package command.
Navigate to the folder where pom.xml is located and execute the command ‘mvn package’.
Upon command successful execution, you can see the jar file ‘micronaut-manual-bean-validation-1-jar-with-dependencies.jar’ in project target folder.$ls ./target/
archive-tmp
classes
generated-sources
maven-archiver
maven-status
micronaut-manual-bean-validation-1-jar-with-dependencies.jar
micronaut-manual-bean-validation-1.jar
test-classes
Execute below command to run the application.
java -jar ./target/micronaut-manual-bean-validation-1-jar-with-dependencies.jar
$java -jar ./target/micronaut-manual-bean-validation-1-jar-with-dependencies.jar
Validating employee with invalid data
age -> must be greater than or equal to 18
name -> must not be blank
id -> must be greater than 0
Validating employee with valid data
emp : Employee [id=1, name=Krishna, age=34]
You can download this application from this link.
No comments:
Post a Comment