title: Custom constraint validator in Spring is not working
body: <p>Was working with the Spring-Boot framework. I was actually defining a "Custom Constraint Validator". But it's just not working for me. </p>
  
  <blockquote>
    <p>pom.xml</p>
  </blockquote>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;com.abcinc.lab&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-lab&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
  
      &lt;name&gt;spring-boot-lab&lt;/name&gt;
      &lt;description&gt;Spring Boot Lab&lt;/description&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.0.0.RC1&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-webflux&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
              &lt;artifactId&gt;lombok&lt;/artifactId&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;io.projectreactor&lt;/groupId&gt;
              &lt;artifactId&gt;reactor-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
      &lt;repositories&gt;
          &lt;repository&gt;
              &lt;id&gt;spring-snapshots&lt;/id&gt;
              &lt;name&gt;Spring Snapshots&lt;/name&gt;
              &lt;url&gt;https://repo.spring.io/snapshot&lt;/url&gt;
              &lt;snapshots&gt;
                  &lt;enabled&gt;true&lt;/enabled&gt;
              &lt;/snapshots&gt;
          &lt;/repository&gt;
          &lt;repository&gt;
              &lt;id&gt;spring-milestones&lt;/id&gt;
              &lt;name&gt;Spring Milestones&lt;/name&gt;
              &lt;url&gt;https://repo.spring.io/milestone&lt;/url&gt;
              &lt;snapshots&gt;
                  &lt;enabled&gt;false&lt;/enabled&gt;
              &lt;/snapshots&gt;
          &lt;/repository&gt;
      &lt;/repositories&gt;
  
      &lt;pluginRepositories&gt;
          &lt;pluginRepository&gt;
              &lt;id&gt;spring-snapshots&lt;/id&gt;
              &lt;name&gt;Spring Snapshots&lt;/name&gt;
              &lt;url&gt;https://repo.spring.io/snapshot&lt;/url&gt;
              &lt;snapshots&gt;
                  &lt;enabled&gt;true&lt;/enabled&gt;
              &lt;/snapshots&gt;
          &lt;/pluginRepository&gt;
          &lt;pluginRepository&gt;
              &lt;id&gt;spring-milestones&lt;/id&gt;
              &lt;name&gt;Spring Milestones&lt;/name&gt;
              &lt;url&gt;https://repo.spring.io/milestone&lt;/url&gt;
              &lt;snapshots&gt;
                  &lt;enabled&gt;false&lt;/enabled&gt;
              &lt;/snapshots&gt;
          &lt;/pluginRepository&gt;
      &lt;/pluginRepositories&gt;
  
  
  &lt;/project&gt;
  </code></pre>
  
  <blockquote>
    <p>ValidString.java</p>
  </blockquote>
  
  <pre><code>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&lt;?&gt;[] groups() default {};
  
    Class&lt;? extends Payload&gt;[] payload() default {};
  }
  </code></pre>
  
  <blockquote>
    <p>ValidStringValidator.java</p>
  </blockquote>
  
  <pre><code>import javax.validation.ConstraintValidator;
  import javax.validation.ConstraintValidatorContext;
  
  /**
   * @author bineeth
   * @param &lt;A&gt;
   * @param &lt;T&gt;
   *
   */
  public class ValidStringValidator implements ConstraintValidator&lt;ValidString, String&gt; {
  
    @Override
    public void initialize(ValidString constraintAnnotation) {
      ConstraintValidator.super.initialize(constraintAnnotation);
    }
  
    @Override
    public boolean isValid(String data, ConstraintValidatorContext context) {
      return (null != data &amp;&amp; (!data.trim().equals("")));
    }
  }
  </code></pre>
  
  <p>And here is where I'd used the Constraint.</p>
  
  <blockquote>
    <p>SpringBootComponentImpl.java</p>
  </blockquote>
  
  <pre><code>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&lt;String, String&gt; execute(final @ValidString String commandType) {
      return new HashMap&lt;String, String&gt;() {
        {
          put("execution", "success");
        }
      };
    }
  
  }
  </code></pre>
  
  <p>Perhaps might missed some thing. But can't figure it out yet. Appreciate any help. Thank you!</p>