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
@PathVariable("name") @Size(min=1, max=10) String name. Then add remaining code from his post and see if it works. It should.