I'm wondering whether to use matrix or query parameters in my URLs. I found an older discussion to that topic not satisfying.
Examples
- URL with query params: http://some.where/thing?paramA=1¶mB=6542
- URL with matrix params: http://some.where/thing;paramA=1;paramB=6542
At first sight matrix params seem to have only advantages:
- more readable
- no encoding and decoding of "&" in XML documents is required
- URLs with "?" are not cached in many cases; URLs with matrix params are cached
- matrix parameters can appear everywhere in the path and are not limited to its end
- matrix parameters can have more than one value:
paramA=val1,val2
But there are also disadvantages:
- only a few frameworks like JAX-RS support matrix parameters
- When a browser submits a form via GET, the params become query params. So it ends up in two kinds of parameters for the same task. To not confuse users of the REST services and limit the effort for the developers of the services, it would be easier to use always query params - in this area.
Since the developer of the service can choose a framework with matrix param support, the only remaining disadvantage would be that browsers create by default query parameters.
Are there any other disadvantages? What would you do?
In addition to Tim Sylvester's answer I would like to provide an example of how matrix parameters can be handled with JAX-RS .
Matrix parameters at the last resource element
You can access them using the
@MatrixParam
annotationResponse
But like the Javadoc states
... what brings us to point 2
Matrix parameters in the middle of an URL
You can access matrix parameters anywhere using path variables and
@PathParam
PathSegment
.Response
Since the matrix parameters are provided as a
MultivaluedMap
you can access each byor if you only need the first one
Get all matrix parameters as one method parameter
Use a
List<PathSegment>
to get them allResponse
--Too important to be relegated to comment section.--
I'm not sure what the big deal is with matrix URLs. According to the w3c design article that TBL wrote, it was just a design idea and explicitly states that it's not a feature of the web. Things like relative URLs aren't implemented when using it. If you want to use it, that's fine; there's just no standard way to use it because it's not a standard. – Steve Pomeroy
So short answer is, if you need RS for business purpose, you are better off using request parameter.
The important difference is that matrix parameters apply to a particular path element while query parameters apply to the request as a whole. This comes into play when making a complex REST-style query to multiple levels of resources and sub-resources:
It really comes down to namespacing. If only query parameters were used, you would end up with parameters like "category_name" and "object_name" and you would lose the clarity added by the locality of the parameters within the request. In addition, when using a framework like JAX-RS, all the query parameters would show up within each resource handler, leading to potential conflicts and confusion.
If your query has only one "level", then the difference is not really important and the two types of parameters are effectively interchangeable, however query parameters are generally better supported and more widely recognized. In general, I would recommend that you stick with query parameters for things like HTML forms and simple, single-level HTTP APIs.