Hi everyone,
Looking back in the list archives I can see that parameter binding and @MvcBinding was discussed not too long ago. I had a question regarding its implementation: Is there a reason why @MvcBinding is not allowed to be used on a method, such as a getter?
The main reason I'm asking is because I'm working on how to deal with some existing models that I'd like to create simple MVC CRUD forms for. These existing models have bean validation constraint annotations applied, but no decorations for JAX-RS or MVC. I'd like to subclass these existing models to add the necessary @FormParam and @MvcBinding annotations to have the subclasses work as my MVC models. Subclassing and adding @FormParam to a setter method works (see footnote below), but since @MvcBinding isn't allowed on a method I don't have a good way to bind the bean validation annotations on my superclass bean to the MVC subclass.
For example, in Ivar Grimstad's Ozark MVC examples repo on github, this is what the HelloBean model looks like in the hello example:
public class HelloBean {
@MvcBinding
@NotNull
@Size(min = 1, max = 16)
@FormParam("firstName")
private String firstName;
@MvcBinding
@NotNull
@Size(min = 1, max = 24)
@FormParam("lastName")
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
What I'm trying to do is work with a base bean that looks like:
public class HelloCore {
private String firstName;
private String lastName;
@NotNull
@Size(min = 1, max = 16)
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@NotNull
@Size(min = 1, max = 24)
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
And then extend that bean to be an MVC model like:
public class HelloBean extends HelloCore {
@Override
@MvcBinding
public String getFirstName() {
return super.getFirstName();
}
@Override
@FormParam("firstName")
public void setFirstName(String firstName) {
super.setFirstName(firstName);
}
@Override
@MvcBinding
public String getLastName() {
return super.getLastName();
}
@Override
@FormParam("lastName")
public void setLastName(String lastName) {
super.setLastName(lastName);
}
}
But I can't do that due to the restrictions on the @MvcBinding annotation.
I did a quick test, modifying the @MvcBinding annotation to also support methods, and then modifying org.mvcspec.ozark.binding.validate.ConstraintViolations to also check getter methods for @MvcBinding, and that got things working per my example above.
So that's why I'm asking if there is a reason why @MvcBinding isn't currently supported on methods? Or can you think of an alternate approach I should be taking for this sort of operation?
(Footnote about using @FormParam on the setter method like I do in my example - This may not always be portable, as the javadocs for @FormParam say that implementations are only required to support it on method parameters, and not for fields or methods. That said, I've tested with RESTeasy+Wildfly11 and Glassfish5 and both work fine.)
Thanks!
Chris