title: How do I use a custom validator with dropwizard?
body: <p>I have a REST api written by someone else in which the method that handles the request to a particular url accepts a bunch of parameters that are populated from path parameters.</p>
  
  <pre><code>@POST
  @Path("/{classid}/{studentid}/details")
  @Consumes(MediaType.MULTIPART_FORM_DATA)
  @SuppressWarnings("unchecked")
  
  
  public Response processFile(@FormDataParam("sourceFile") InputStream aStream, @PathParam("classid") String classId, @PathParam("studentid")  String studentId, @Context HttpServletRequest httpRequest) {
  
  // Code to do stuff and return a response
  }
  </code></pre>
  
  <p>The person who wrote this has used DropWizard and I have no previous experience working on it. I have the task of validating the studentId field by comparing it with values in the db. This would be pretty straightforward but I have been told to do it using a custom validator. I am pretty new to writing annotations but after much digging wrote an annotation like this,</p>
  
  <pre><code>@Target(ElementType.PARAMETER)
  @Retention(RetentionPolicy.RUNTIME)
  @Documented
  @Constraint(validatedBy = StudentIdValidator.StudentIdValidation.class)
  
  public @interface StudentIdValidator {
  
      String message() default "{Invalid Id}";
  
        Class&lt;?&gt;[] groups() default {};
  
        Class&lt;? extends Payload&gt;[] payload() default {};
  
  
        class StudentIdValidation implements ConstraintValidator&lt;StudentIdValidator, String&gt; {
  
          @Override
          public void initialize(StudentIdValidator constraintAnnotation) {
              System.out.println("Annotation initialize !!!!!!!!!");
          }
  
  
          @Override
          public boolean isValid(String value, ConstraintValidatorContext context)                                                                            {
                  // TODO Auto-generated method stub
                  System.out.println("Annotation called");
                  return false;
              }
          }
      }
  </code></pre>
  
  <p>After this I added the annotation to the field I wanted to run the validation on like this,</p>
  
  <pre><code>public Response processFile(@FormDataParam("sourceFile") InputStream     aStream, @PathParam("classid") String classId, @StudentIdValidator     @PathParam("studentid")  String studentId, @Context HttpServletRequest     httpRequest)
  </code></pre>
  
  <p>Now the problem is that, when the I run/debug the code...this validator is not being called, also I have no idea how to get the value of studentId inside the studentId validation class. So I dug some more and added this to the application file</p>
  
  <pre><code>class MyApplication extends Application&lt;MyConfiguration&gt; {
      ........
  
      @Override
     public void run(MyConfiguration myConfiguration, Environment                     currentEnvironment) {
  
       currentEnvironment.jersey().register(StudentIdValidator.class);
  
      }
  </code></pre>
  
  <p>I am literally at the end of my wits. Any help will be very VERY appreciated. Sorry about the poor formatting.</p>