I am thinking of two options for handling query/request parameters:
- Map individual parameters to corresponding the method parameters:
@GET
public String blah(@QueryParam("testParam") String testParam) {
}
- Map all parameters to the properties of a Java bean:
@GET
public String blah(@BeanParam RequestParamBean bean) {
}
The second option seems more attractive as it allows the validation logic of input query parameters to be moved and decoupled from the blah
method whose core responsibility should be to process and delegating the validation to a validator should high degree of decoupling (and also SOLID principle, right?).
However, most of the examples I see (in fact, the existing project I am working on) use only the first option. I am wondering if is there any reason why the second option is not widely used? Are there any pitfalls? Is this an anti-pattern? Is this against any best practice?