Let's take a look at @ queryparam
First look at the example:
Java code
- PATH ("/users ")
- Public class userservice {
- @ Get
- @ Path ("/query ")
- Public Response getusers (
- @ Queryparam ("from") int from,
- @ Queryparam ("to") int,
- @ Queryparam ("orderby") List <string> orderby ){
- Return response
- . Status (200)
- . Entity ("getusers is called, from:" + from + ", to:" +
- + ", Orderby" + orderby. tostring (). Build ();
- }
- }
Path("/users")public class UserService { @GET@Path("/query")public Response getUsers(@QueryParam("from") int from,@QueryParam("to") int to,@QueryParam("orderBy") List<String> orderBy) { return Response .status(200) .entity("getUsers is called, from : " + from + ", to : " + to+ ", orderBy" + orderBy.toString()).build(); } }
URL input: users/query? From = 100 & to = 200 & orderby = age & orderby = Name
In this case, the output is:
Getusers is called, from: 100, to: 200, orderby [age, name]
Note that, unlike @ pathparam, @ queryparam
The specified parameter in the URL is in the form of a key-value pair, and in the program
@ Queryparam ("from") int from reads the from Value in the URL,
In @ pathparem, only the parameter value is displayed in the URL, and no key-value pair is displayed, for example:
"/Users/2011/06/30"
Then:
Java code
- @ Get
- @ Path ("{year}/{month}/{day }")
- Public Response getuserhistory (
- @ Pathparam ("year") int year,
- @ Pathparam ("month") int month,
- @ Pathparam ("day") int day ){
- String date = year + "/" + month + "/" + Day;
- Return response. Status (200)
- . Entity ("getuserhistory is called, year/month/day:" + date)
- . Build ();
- }
@GET@Path("{year}/{month}/{day}")public Response getUserHistory(@PathParam("year") int year,@PathParam("month") int month, @PathParam("day") int day) { String date = year + "/" + month + "/" + day; return Response.status(200).entity("getUserHistory is called, year/month/day : " + date).build(); }
Output:
Getuserhistory is called, year/month/day: 2011/6/30
2. Get it in a dynamic way:
Java code
- @ Path ("/users ")
- Public class userservice {
- @ Get
- @ Path ("/query ")
- Public Response getusers (@ context uriinfo info ){
- String from = info. getqueryparameters (). getfirst ("from ");
- String to = info. getqueryparameters (). getfirst ("");
- List <string> orderby = info. getqueryparameters (). Get ("orderby ");
- Return response
- . Status (200)
- . Entity ("getusers is called, from:" + from + ", to:" +
- + ", Orderby" + orderby. tostring (). Build ();
- }
@Path("/users")public class UserService { @GET@Path("/query")public Response getUsers(@Context UriInfo info) { String from = info.getQueryParameters().getFirst("from");String to = info.getQueryParameters().getFirst("to");List<String> orderBy = info.getQueryParameters().get("orderBy"); return Response .status(200) .entity("getUsers is called, from : " + from + ", to : " + to+ ", orderBy" + orderBy.toString()).build(); }
URL; users/query? From = 100 & to = 200 & orderby = age & orderby = Name
Output:
Getusers is called, from: 100, to: 200, orderby [age, name]
Note that the two parameters after orderby are read as list.
3 @ defaultvalue, default value
Example:
Java code
- @ Path ("/users ")
- Public class userservice {
- @ Get
- @ Path ("/query ")
- Public Response getusers (
- @ Defaultvalue ("1000") @ queryparam ("from") int from,
- @ Defaultvalue ("999") @ queryparam ("to") int,
- @ Defaultvalue ("name") @ queryparam ("orderby") List <string> orderby ){
- Return response
- . Status (200)
- . Entity ("getusers is called, from:" + from + ", to:" +
- + ", Orderby" + orderby. tostring (). Build ();
- }
@Path("/users")public class UserService { @GET@Path("/query")public Response getUsers(@DefaultValue("1000") @QueryParam("from") int from,@DefaultValue("999")@QueryParam("to") int to,@DefaultValue("name") @QueryParam("orderBy") List<String> orderBy) { return Response .status(200) .entity("getUsers is called, from : " + from + ", to : " + to+ ", orderBy" + orderBy.toString()).build(); }
URL: users/Query
Output: getusers is called, from: 1000, to: 999, orderby [name]
Good understanding.