Follow below step-by-step procedure to work with custom constraints in Micronaut.
Step 1: Define the constraint interface.
EmployeeNameConstraint.java
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = { EmployeeNameConstraintValidator.class })
public @interface EmployeeNameConstraint {
String message() default "Employee name is not matched";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
int minSize() default 10;
String regex();
}
Step 2: Define EmployeeNameConstraintValidator class.
EmployeeNameConstraintValidator.java
@Singleton
public class EmployeeNameConstraintValidator implements ConstraintValidator<EmployeeNameConstraint, String> {
@Override
public boolean isValid(String value, AnnotationValue<EmployeeNameConstraint> annotationMetadata,
io.micronaut.validation.validator.constraints.ConstraintValidatorContext context) {
if (value == null) {
return false;
}
Map<String, Object> parameters = annotationMetadata.getConvertibleValues().asMap();
Integer minimumSize = Integer.parseInt(parameters.get("minSize").toString());
String regEx = parameters.get("regex").toString();
if (value.length() < minimumSize) {
return false;
}
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(value);
return matcher.matches();
}
}
Find the below working application.
Step 1: Create new maven project ‘micronaut-custom-constraint’.
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-custom-constraint</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-plugin.version>3.8.1</maven-compiler-plugin.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 class.
Employee.java
package com.sample.app.model;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;
import com.sample.app.constraint.EmployeeNameConstraint;
import io.micronaut.core.annotation.Introspected;
@Introspected
public class Employee {
@Positive
private int id;
@EmployeeNameConstraint(minSize = 2, regex = "[a-zA-Z ]*", message = "Employee name must be >= 2 characters and contain only a-z, A-Z characters")
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 EmployeeNameConstraint class.
EmployeeNameConstraint.java
package com.sample.app.constraint;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
import com.sample.app.constraint.validators.EmployeeNameConstraintValidator;
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = { EmployeeNameConstraintValidator.class })
public @interface EmployeeNameConstraint {
String message() default "Employee name is not matched";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
int minSize() default 10;
String regex();
}
Step 5: Define EmployeeNameConstraintValidator class.
EmployeeNameConstraintValidator.java
package com.sample.app.constraint.validators;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.sample.app.constraint.EmployeeNameConstraint;
import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.validation.validator.constraints.ConstraintValidator;
import jakarta.inject.Singleton;
@Singleton
public class EmployeeNameConstraintValidator implements ConstraintValidator<EmployeeNameConstraint, String> {
@Override
public boolean isValid(String value, AnnotationValue<EmployeeNameConstraint> annotationMetadata,
io.micronaut.validation.validator.constraints.ConstraintValidatorContext context) {
if (value == null) {
return false;
}
Map<String, Object> parameters = annotationMetadata.getConvertibleValues().asMap();
Integer minimumSize = Integer.parseInt(parameters.get("minSize").toString());
String regEx = parameters.get("regex").toString();
if (value.length() < minimumSize) {
return false;
}
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(value);
return matcher.matches();
}
}
Step 6: Define EmployeeService class.
EmployeeService.java
package com.sample.app.service;
import com.sample.app.constraint.EmployeeNameConstraint;
import jakarta.inject.Singleton;
@Singleton
public class EmployeeService {
public void registerEmployee(
@EmployeeNameConstraint(minSize = 2, regex = "[a-zA-Z ]*", message = "Employee name must be >= 2 characters and contain only a-z, A-Z characters") String name) {
System.out.println("Employee registered successfully " + name);
}
}
Step 7: Define main application class.
App.java
package com.sample.app;
import com.sample.app.model.Employee;
import com.sample.app.service.EmployeeService;
import io.micronaut.context.ApplicationContext;
public class App {
public static void main(String[] args) {
try (ApplicationContext applicationContext = ApplicationContext.run()) {
EmployeeService employeeService = applicationContext.getBean(EmployeeService.class);
Employee emp = new Employee();
emp.setAge(25);
emp.setId(1);
emp.setName("a");
employeeService.registerEmployee("a");
}
}
}
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-custom-constraint-1-jar-with-dependencies.jar’ in project target folder.
$ls ./target/
archive-tmp
classes
generated-sources
maven-archiver
maven-status
micronaut-custom-constraint-1-jar-with-dependencies.jar
micronaut-custom-constraint-1.jar
test-classes
Execute below command to run the application.
java -jar ./target/micronaut-custom-constraint-1-jar-with-dependencies.jar
$java -jar ./target/micronaut-custom-constraint-1-jar-with-dependencies.jar
Exception in thread "main" javax.validation.ConstraintViolationException: registerEmployee.name: Employee name must be >= 2 characters and contain only a-z, A-Z characters
at io.micronaut.validation.ValidatingInterceptor.intercept(ValidatingInterceptor.java:111)
at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:137)
at com.sample.app.service.$EmployeeService$Definition$Intercepted.registerEmployee(Unknown Source)
at com.sample.app.App.main(App.java:18)
You can download this application from this link.
No comments:
Post a Comment