@QueryParam

The @javax.ws.rs.QueryParam annotation injects the value(s) of a HTTP query parameter into a class field, a resource class bean property (the getter method for accessing the attribute), or a method parameter.

The following example illustrates the use of @QueryParam to extract the value of the desired query parameter present in the URI. This example extracts the value of the query parameter, name, from the request URI and copies the value into the deptName method parameter. The URI that accesses the IT department resource looks like /departments?name=IT:

@GET 
@Produces(MediaType.APPLICATION_JSON) 
public List<Department>  
  findAllDepartmentsByName(@QueryParam("name") String deptName) { 
    List<Department> depts= findAllMatchingDepartmentEntities  
      (deptName);  
  return depts;        
} 

The following example illustrates the use of @QueryParam to extract the value of the query parameter, locationId, from the request URI and copies the value into the deptLocationId class field. The URI that accesses the IT department resource looks like /departments/queryByLocation?locationId=1700:

//Usage of QueryParam for injecting class field
@QueryParam("locationId")
Short deptLocationId;

/**
* Returns list of departments by location
*
* @param locationId
* @return
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("queryByLocation")
public List<Departments> findAllDepartmentsByLocation() {
//Find all departments from the data store
Query queryDepartmentsByLocation =
entityManager.createNamedQuery("Departments.findByLocationId");
queryDepartmentsByLocation.setParameter("locationId", deptLocationId);
List<Departments> departments = queryDepartmentsByLocation.getResultList();
logger.log(Level.INFO, departments.toString());
return departments;
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.99.152