Java Reference
In-Depth Information
Accessing Query Parameters
Problem
You want to access the query parameters in the URI (the name=value pairs following a ? )
within your method implementation.
Solution
Use the @QueryParam annotation on your method parameter. Optionally, include the @De-
faultValue in case the query parameter you're expecting is not passed.
Discussion
The use of query parameters is prevalent in the REST world, and JAX-RS makes it easy to
access them. All you need to do in your code is create a parameter on your implementation
method that you want to hold the specific query parameter value you need access to. If the
user does not access your resource with that parameter, you can specify a default value so that
things don't go haywire. See Example 8-10 .
Example8-10.Accessing a query parameter value
package com.soacookbook;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
/**
* Shows accessing Query Parameters.
*/
@Path("/products")
public class ProductQuery {
@Context
private UriInfo context;
/** Creates a new instance of ProductResource */
public ProductQuery() { }
Search WWH ::




Custom Search