title: Add class level validation to @Pathvariable in Spring
body: <p>I have done a lot of research and tried a lot of solutions available before posting this question. </p>
  
  <p>Here is the tricky situation I am stuck in. </p>
  
  <p>I have a Spring controller that has multiple request mappings and they all have @PathVariables </p>
  
  <p>Here is what Controller looks like: </p>
  
  <pre><code>@Controller
  @EnableWebMvc
  public class ChildController extends ParentController&lt;InterfaceController&gt; implements InterfaceController{
  
      @Override
      @RequestMapping(value = "/map/{name}", produces = "application/json; charset=UTF-8", method = RequestMethod.GET)
      @ResponseStatus( HttpStatus.OK)
      @ResponseBody   
      public List&lt;Friends&gt; getAllFriendsByName(
          @PathVariable("name") String name,
          @RequestParam(value="pageSize", required=false) String pageSize,
          @RequestParam(value="pageNumber", required=false) String pageNumber,            
          HttpServletRequest request) throws BasicException {
  
      //Some logic over here;
  
      return results;
      }
  
  @Override
      @RequestMapping(value = "/map/{id}", produces = "application/json; charset=UTF-8", method = RequestMethod.GET)
      @ResponseStatus( HttpStatus.OK)
      @ResponseBody   
      public List&lt;Friends&gt; getAllFriendsById(
          @PathVariable("id") String id,
          @RequestParam(value="pageSize", required=false) String pageSize,
          @RequestParam(value="pageNumber", required=false) String pageNumber,            
          HttpServletRequest request) throws BasicException {
  
      //Some logic over here;
  
      return results;
      }
  
  }
  </code></pre>
  
  <p>Now I have recently learned that you cannot validate a Path Variable in Spring, and contrary to these examples which I have tried it turns out that it does not work
  (Examples tried: <a href="https://stackoverflow.com/questions/35403744/pathvariable-validation-in-spring-4">@PathVariable Validation in Spring 4</a>)</p>
  
  <p>So I thought of adding a custom JSR303 constraint validation to my path variables and create something of my own like </p>
  
  <pre><code> import static java.lang.annotation.ElementType.TYPE;
   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;
  
   @Target({ TYPE})
   @Retention(RUNTIME)
   @Constraint(validatedBy = PathVariableValidator.class)
   @Documented
   public @interface RequestValidationNeeded {
  
  String id();
  
  String name();
  
  Class&lt;?&gt;[] constraints() default {};
  
  Class&lt;?&gt;[] groups() default {};
  
  Class&lt;? extends Payload&gt;[] payload() default {};
  
  String message() default "totally wrong, dude!";
  
   }
  </code></pre>
  
  <p>This is not my original idea its inspired from this post
  <a href="http://techblogs4u.blogspot.ca/2012/09/method-parameter-validation-in-spring-3.html" rel="nofollow noreferrer">http://techblogs4u.blogspot.ca/2012/09/method-parameter-validation-in-spring-3.html</a></p>
  
  <p>What I am not able to figure out is how I will map @RequestValidationNeeded annotation to both PathVriables i.e. <code>id</code> and <code>name</code> and call the same validator and based on which field is calling the validator invoke the appropriate validation. </p>
  
  <p>I do not like this way but my options have exhausted and this is the last thing that I could think of that might work but even for this I need to figure out a way to annotate both path variables with same annotation and call same validator. </p>
  
  <p>I would appreciate any kind of help or direction on this. </p>
  
  <p>Some other questions that I have posted based on what I have tried might help you understand better what validations I have tried. </p>
  
  <p>P.S. I am posting links instead of the code just to keep the question short.</p>
  
  <p>Links: 
  <a href="https://stackoverflow.com/questions/37849617/how-to-make-customeditor-in-spring-dynamic-enough-to-call-validation-on-appropri">How to make customEditor in Spring dynamic enough to call validation on appropriate methods</a></p>
  
  <p><a href="https://stackoverflow.com/questions/37849150/spring-rest-service-controller-not-being-validate-by-pathvariable-and-valid">Spring REST Service Controller not being validate by @PathVariable and @Valid</a> </p>