I am using @valid and @initbinder for validation of the data being passed to the service but I am facing an issue that @InitBinder
is working only globally,i.e.
@a-better-oliver's answer is great, but below is an another approach to the same problem with Spring, more verbose but typesafe:
@InitBinder
protected void initBinder(WebDataBinder binder){
if (binder.getTarget() != null
&& LoginRequest.class.equals(binder.getTarget().getClass())) {
binder.setValidator(new LoginRequestValidator());
}
}
This way we do not rely on the hard-coded string and also do not care how Spring exposes unnamed attributes.