I have the following GET request in my controller:
@Controller
public class TestController {
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new ProfileTokenValidator());
}
@RequestMapping(value = "/more/{fromLocation:.+}/to/{toLocation:.+}", method = RequestMethod.GET)
@ResponseBody
public void copyProfile(@PathVariable @Valid String fromLocation, @PathVariable String toLocation) {
...
}
}
And I have a simple validator for string fromLocation
public class ProfileTokenValidator implements Validator{
@Override
public boolean supports(Class validatedClass) {
return String.class.equals(validatedClass);
}
@Override
public void validate(Object obj, Errors errors) {
String location = (String) obj;
if (location == null || location.length() == 0) {
errors.reject("destination.empty", "Destination should not be empty.");
}
}
}
The problem that I need to provide validation for case, when fromLocation is the same as toLocation.
Please help with advice or something, is there any way to write validator which will check both parameters simultaniously for Get request?
Thanks.
Blockquote
That was a bad idea. I went another way and created simple method in controller that validates my parameters. If something is wrong it throws special exception, wich handles by written handler. This handler returns 400 status bad request and message defined before throwing. So it acts exactly like custom validator. A great help was from the article by this link http://doanduyhai.wordpress.com/2012/05/06/spring-mvc-part-v-exception-handling/
And the following is my code: