Parameters of a resource method is annotated with parameter-based annotations to extract information from a request. A previous example presented the use @PathParam to extract a path parameter from the path component of the request URL That matched the path declared in @Path.
@QueryParam is used to extract query parameters from the query component of the request URL. The following example is a extract from the Sparklines sample:
@Path ("Smooth") @GET PublicResponse Smooth (@DefaultValue ("2") @QueryParam ("step")intstep, @DefaultValue ("true") @QueryParam ("Min-m")Booleanhasmin, @DefaultValue ("true") @QueryParam ("Max-m")BooleanHasmax, @DefaultValue ("true") @QueryParam ("Last-m")BooleanHaslast, @DefaultValue ("Blue") @QueryParam ("Min-color")) Colorparam Mincolor, @DefaultValue ("Green") @QueryParam ("Max-color")) Colorparam Maxcolor, @DefaultValue ("Red") @QueryParam ("Last-color")) Colorparam lastcolor) {...}
If a query parameter "step" exists in the query component of the request URI then the "step" value would be extracted and parsed as a, signed integer and assigned to the step method parameter. If "Step" does not exist then a default value of 2, as declared in the @DefaultValue annotation, would be assigned To the step method parameter. If the "step" value cannot be parsed as a + signed integer then an HTTP 404 (Not Found) response is returned. User defined Java types such as Colorparam may used, which as implemented as follows:
Public classColorparamextendsColor { PublicColorparam (String s) {Super(getRGB (s)); } Private Static intgetRGB (String s) {if(S.charat (0) = = ' # ') { Try{Color C= Color.decode ("0x" + s.substring (1)); returnC.getrgb (); } Catch(NumberFormatException e) {Throw NewWebapplicationexception (400); } } Else { Try{Field F= Color.class. GetField (s); return((Color) F.get (NULL) . getRGB (); } Catch(Exception e) {Throw NewWebapplicationexception (400); } } }}
In general the Java type of the method parameter may:
- be a primitive type;
- Has a constructor that accepts a single
String
argument;
- There is a static method named
valueOf
or that fromString
accepts a single String
argument (see, for example, Integer.valueOf(String)
);
- Be
List<T>
, Set<T>
or SortedSet<T>
, where T
satisfies 2 or 3 above. The resulting collection is read-only.
Sometimes parameters may contain more than one value for the same name. If This is the case then types in 4) may used to obtain all values.
If The @DefaultValue is not used in conjunction with @QueryParam and the query parameter are not present in the request the n value would be a empty collection for List, Set or SortedSet, NULL for other object types, and the java-defined default For primitive types.
The @PathParam and the other parameter-based annotations, @MatrixParam, @HeaderParam, @ Cookieparam, @FormParam obey the same rules as @QueryParam. @MatrixParam extracts information from URL of path segments. @HeaderParam extracts information from the HTTP headers. @Co Okieparam extracts information from the cookies declared in cookie related HTTP headers.
@FormParam is slightly special because it extracts information from a request representation This is the MIME media Type "application/x-www-form-urlencoded" and conforms to the encoding specified by HTML forms, as described here. This parameter was very useful for extracting information that's POSTed by HTML forms, for example the following extracts The form parameter named "name" from the POSTed form data:
@POST @consumes ("application/x-www-form-urlencoded") Public void Post (@FormParam ("name") String name) { // Store the message}
If it is necessary to obtain a general map of parameter name to values then, for query and path parameters it is possible To do the following:
@GET Public String Get (@Context Uriinfo UI) { multivaluedmap<string, string> queryparams = Ui.getqueryparameters (); Multivaluedmap<string, string> pathparams = ui.getpathparameters ();}
For header and cookie parameters the following:
@GET Public String Get (@Context httpheaders hh) { multivaluedmap<string, string> headerparams = Hh.getrequestheaders (); Map<string, cookie> pathparams = hh.getcookies ();}
In general @Context can is used to obtain contextual Java types related to the request or response. For form parameters It's possible to do the following:
@POST @consumes ("application/x-www-form-urlencoded") Public void Post (multivaluedmap<string, string> formparams) { // Store the Message}
Jersey (1.19.1)-Extracting Request Parameters