Annotation | Description |
|---|---|
@Context | Injects information into a class field, bean property, or method parameter |
@CookieParam | Extracts information from cookies declared in the cookie request header |
@FormParam | Extracts information from a request representation whose content type is application/x-www-form-urlencoded |
@HeaderParam | Extracts the value of a header |
@MatrixParam | Extracts the value of a URI matrix parameter |
@PathParam | Extracts the value of a URI template parameter |
@QueryParam | Extracts the value of a URI query parameter |
@Path(/employees/"{firstname}.{lastname}@{domain}.com")
public class EmpResource {
@GET
@Produces("text/xml")
public String getEmployeelastname(@PathParam("lastname") String lastName) {
...
}
}
In this example, the @Path annotation defines the URI variables (or path parameters) {firstname} , {lastname}, and {domain}. The @PathParam in the method parameter of the request method extracts the last name from the email address.@Path(/employees/{"firstname}.{lastname[a-zA-Z]*}@{domain}.com")
If the last name does not match the regular expression, a 404 response is returned.@Path(/employees/")
@GET
public Response getEmployees(
@DefaultValue("2002") @QueryParam("minyear") int minyear,
@DefaultValue("2010") @QueryParam("maxyear") int maxyear)
{...}
This code snippet defines two query parameters, minyear and maxyear. The following HTTP request would query for all employees who have joined between 1999 and 2009:<form action="http://example.com/employees/" method="post"> <fieldset> Employee name: <input name="empname" tabindex="1" type="text" /> Employee address: <input name="empaddress" tabindex="2" type="text" /> Manager name: <input name="managername" tabindex="3" type="text" /> </fieldset> </form>Use the following code snippet to extract the manager name from this HTML form:
@POST
@Consumes("application/x-www-form-urlencoded")
public void post(@FormParam("managername") String managername) {
// Store the value
...
}
To obtain a map of form parameter names to values, use a code snippet like the following:@POST
@Consumes("application/x-www-form-urlencoded")
public void post(MultivaluedMap<String. String> formParams) {
// Store the message
}
@GET
public String getParams(@Context UriInfo ui) {
MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
MultivaluedMap<String, String> pathParams = ui.getPathParameters();
}
The javax.ws.rs.core.HttpHeaders interface provides information about request headers and cookies. The following code snippet shows how to obtain a map of header and cookie parameter names to values:@GET
public String getHeaders(@Context HttpHeaders hh) {
MultivaluedMap<String, String> headerParams = hh.getRequestHeaders();
MultivaluedMap<String, Cookie> pathParams = hh.getCookies();
}
Labels: REST