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;
As stated in this Oracle documentation:
Example (drawn from here):
See following URI patterns and result:
URI Pattern : “/books/2012/”
getBooks is called, year : 2012, author : null, country : null
URI Pattern : “/books/2012;author=andih”
getBooks is called, year : 2012, author : andih, country : null
URI Pattern : “/books/2012;author=andih;country=germany”
getBooks is called, year : 2012, author : andih, country : germany
URI Pattern : “/books/2012;country=germany;author=andih”
getBooks is called, year : 2012, author : andih, country : germany
For an explanation of the difference you may have a look at URL matrix parameters vs. request parameters
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,
In above example, first Matrix Param
type=fruits
is applying to only grocery resource samerange=300
is applying to only price resource but Query Param for paginationlimit=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.