Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between QueryParam and MatrixParam in JAX-RS?

Tags:

java

jax-rs

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;

like image 967
jiafu Avatar asked Apr 17 '12 01:04

jiafu


People also ask

What is @PathParam and @QueryParam?

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?

What is @MatrixParam?

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.

What is QueryParam in rest?

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.

Is QueryParam optional?

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.


1 Answers

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.

like image 70
Hardik Patel Avatar answered Oct 11 '22 09:10

Hardik Patel