2

I have done a lot of research and tried a lot of solutions available before posting this question.

Here is the tricky situation I am stuck in.

I have a Spring controller that has multiple request mappings and they all have @PathVariables

Here is what Controller looks like:

@Controller
@EnableWebMvc
public class ChildController extends ParentController<InterfaceController> implements InterfaceController{

    @Override
    @RequestMapping(value = "/map/{name}", produces = "application/json; charset=UTF-8", method = RequestMethod.GET)
    @ResponseStatus( HttpStatus.OK)
    @ResponseBody   
    public List<Friends> 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<Friends> 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;
    }

}

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: @PathVariable Validation in Spring 4)

So I thought of adding a custom JSR303 constraint validation to my path variables and create something of my own like

 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<?>[] constraints() default {};

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

String message() default "totally wrong, dude!";

 }

This is not my original idea its inspired from this post http://techblogs4u.blogspot.ca/2012/09/method-parameter-validation-in-spring-3.html

What I am not able to figure out is how I will map @RequestValidationNeeded annotation to both PathVriables i.e. id and name and call the same validator and based on which field is calling the validator invoke the appropriate validation.

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.

I would appreciate any kind of help or direction on this.

Some other questions that I have posted based on what I have tried might help you understand better what validations I have tried.

P.S. I am posting links instead of the code just to keep the question short.

Links: How to make customEditor in Spring dynamic enough to call validation on appropriate methods

Spring REST Service Controller not being validate by @PathVariable and @Valid

2
  • In the solution provided by Patrick (stackoverflow.com/questions/35403744/…) he told specifically to add @Validated annotation to the controller and also add @Size annotation to your path variable, for example: @PathVariable("name") @Size(min=1, max=10) String name. Then add remaining code from his post and see if it works. It should. Commented Jun 16, 2016 at 6:56
  • @R4J I did try that. The code that is posted is my local code, not the one that I tried. I did add the @ Validated and added the @ Size validation as well. Commented Jun 16, 2016 at 13:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.