Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use @QueryParam or @BeanParam in JAX-RS?

Tags:

java

jax-rs

I am thinking of two options for handling query/request parameters:

  1. Map individual parameters to corresponding the method parameters:
@GET
public String blah(@QueryParam("testParam") String testParam) {

}
  1. 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?

like image 860
user1539343 Avatar asked Jul 06 '16 04:07

user1539343


People also ask

What's the difference between @PathParam and @QueryParam restful annotations?

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.

What is@ BeanParam annotation?

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.

What is the @QueryParam annotation?

The @QueryParam annotation allows you to map a URI query string parameter or url form encoded parameter to your method invocation.

What is @QueryParam in REST API?

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.


1 Answers

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.

like image 141
cassiomolin Avatar answered Oct 21 '22 21:10

cassiomolin