Issue
Was working with the Spring-Boot framework. I was actually defining a "Custom Constraint Validator". But it's just not working for me.
pom.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.abcinc.lab</groupId>
<artifactId>spring-boot-lab</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-lab</name>
<description>Spring Boot Lab</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RC1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</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>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
ValidString.java
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
/**
* @author bineeth
*
*/
@Documented
@Retention(RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Constraint(validatedBy = ValidStringValidator.class)
public @interface ValidString {
String message() default "Invalid string!";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
ValidStringValidator.java
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
* @author bineeth
* @param <A>
* @param <T>
*
*/
public class ValidStringValidator implements ConstraintValidator<ValidString, String> {
@Override
public void initialize(ValidString constraintAnnotation) {
ConstraintValidator.super.initialize(constraintAnnotation);
}
@Override
public boolean isValid(String data, ConstraintValidatorContext context) {
return (null != data && (!data.trim().equals("")));
}
}
And here is where I'd used the Constraint.
SpringBootComponentImpl.java
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Component;
import com.abcinc.lab.springbootlab.validation.ValidString;
/**
* @author bineeth
*
*/
@Component
public class SpringBootComponentImpl implements SpringBootComponent {
@SuppressWarnings("serial")
@Override
public Map<String, String> execute(final @ValidString String commandType) {
return new HashMap<String, String>() {
{
put("execution", "success");
}
};
}
}
Perhaps might missed some thing. But can't figure it out yet. Appreciate any help. Thank you!
Solution
You're missing two things:
- There is already a standard NotBlank annotation doing what your custom validation does
- A Spring beans's method parameters will not be validated if you don't annotated the bean with
@Validated
.
Answered By - JB Nizet
Answer Checked By - Senaida (JavaFixing Volunteer)