I am thinking of two options for handling query/request parameters:
@GET
public String blah(@QueryParam("testParam") String testParam) {
}
@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?
PathParam could be used to drill down to entity class hierarchy. Whereas, QueryParam could be reserved for specifying attributes to locate the instance of a class.
BeanParam annotation allows you to inject all the matching request parameters into a single bean object. The @BeanParam annotation can be set on a class field, a resource class bean property (the getter method for accessing the attribute), or a method parameter.
The @QueryParam annotation allows you to map a URI query string parameter or url form encoded parameter to your method invocation.
Basically, @QueryParam denotes that the value of the Query Parameter with the corresponding name will be parsed, and if parsed correctly it will be available on the method argument denoted with @QueryParam . There are baically two ways to pass parameters in a GET request in REST services.
The @BeanParam
annotation was introduced in JAX-RS 2.0 as a parameter aggregator (which means it cannot be used in JAX-RS 1.0).
The idea behind the @BeanParam
annotation is to have a Java class to aggregate parameters annotated with @XxxParam
annotations. The following @XxxParam
annotations can be used to annotate the fields of a parameter aggregator class:
@CookieParam
@FormParam
@HeaderParam
@MatrixParam
@PathParam
@QueryParam
Besides fields annotated the @XxxParam
annotations, the parameter aggregator class can have fields annotated with the @Context
annotation. For a list of types that can be injected with the @Context
annotation, check this answer.
I believe it's just a matter of convenience and preference of the developers. In many situations, a class to aggregate parameters is not necessary. The use of the @XxxParam
annotations in method parameters is very handy.
But when you need to reuse parameters in different methods or the method has many parameters annotated with @XxxParam
annotations, go for the @BeanParam
approach.
In your question, you mentioned the SOLID principle. But don't forget the KISS principle :)
Start with the @XxxParam
annotations in your method parameters and don't overuse the @BeanParam
annotation trying to solve a problem you don't have. You always can refactor your code to create a parameter aggregator class if you need it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With