What is the difference between @PathParam and @QueryParam in REST?
Answer:
Query parameters are added to the url after the ? mark, while a path parameter is part of the regular URL.
@PathParam and @QueryParam are JAX-RS annotations.
They are only available to JAX-RS resource handlers, which technically are not servlets but are very similar.
In a JAX-RS resource handler, one defines the endpoints processed by the handler using @Path annotations. For example @Path(/foo/bar). It is common practice for a single resource handler to handle what I like to call an endpoint node;
it handles multiple endpoints that vary in one path qualifier.
@Path(/foo/bar/a) for one the method that handles 'a'
@Path(/foo/bar//b) for the method that handles 'b'
Often times, we like to consolidate so we might choose to have a single method handle both endpoints. We can use a @PathParam to handle this.
@Path(/foo/bar/{type}) where type is the path parameter.
The handler method signature would add this to assign the type path parameter to a local variable that can be used in the method declaration:
@PathParam("type") String reqestType
JAX-RS automatically assignes the 'type' value of the path from the request to the requestType variable.
@QueryParameter is handled differently in that it is only specified as part of the method declaration and is not included in @Path. For example,
@QueryParam("subtype") String subType
could be added to the parameter list of the handler method to which the subtype query parameter is applicable.
JAX-RS automatically assignes the query parameter value from the request to the subType variable.