I have a hard time believing I'm the only one who wants to do this, but I can't find any references to help me over my hurdle. Using Spring MVC and annotation-based validation (I'm using framework 4.0 and Java 1.7), consider a simple class hierarchy, as follows:
abstract class Foo {
@Size(max=10, message = "The name has to be 10 characters or less.")
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
class Bar extends Foo {
}
class Bang extends Foo {
}
If I put a name in an instance of either Bar or Bang that's greater than 10 characters, I get the validation error I'm expecting. Let's suppose, though, that I still want Bar and Bang to be derived from the abstract base class Foo, but that I want the name attribute of the child classes to have different validations.
How do I annotate Bar and Bang so that Bar.name has a max length of, say, 12 characters while Bang.name has a max length of 8 characters?
Thanks much, Rob