What's the difference between the JAX-RS @QueryParam
and @MatrixParam
? From the documents.The queryparam and matrixparam both can location one resource in special condition. So what's the use case difference?
ps:
Queryparam:
url ? key=value;
Matrixparam
url; key=value;
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. For example, /Vehicle/Car?
Annotation Type MatrixParam A default value can be specified using the DefaultValue annotation. Note that the @MatrixParam annotation value refers to a name of a matrix parameter that resides in the last matched path segment of the Path -annotated Java structure that injects the value of the matrix parameter.
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.
Any optional parameters should certainly be query string parameters. So in our example the resource should be http://example.com/api/orders?stateCode=TX . Followings are the details how we are going to do handle such request on server side using JAX-RS API: @QueryParam is used to handle query parameters.
The
@MatrixParam
annotation will apply to particular Resource present in URL and@QueryParam
will apply to whole Request URL.
Take a example of any Supermarket, If you want all fruits which will be satisfied multiple conditions like type=fruits and price range starts from 300 and list out matching 10 fruits, you can go for below API Design,
http://dev.brandstore.com/inventory/grocery;type=fruits/price;range=300/?limit=10
In above example, first Matrix Param type=fruits
is applying to only grocery resource same range=300
is applying to only price resource but Query Param for pagination limit=10
is applying to whole Request URL. And yes, If only query parameters were used, you would end up with parameters like "grocery_type" and "grocery_price" and you would lose the clarity added by the locality of the parameters within the request.
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