1 See @queryparam first.
First look at the example:
Java code
Path ("/users") Public classUserService {@GET @Path ("/query") PublicResponse getusers (@QueryParam ("From")intFrom , @QueryParam ("To")intto, @QueryParam ("List<string>")) { returnResponse. Status (200). Entity (Getusers is called, from: ' + from + ', to: ' + to+ ", by" +orderby.tostring ()). build (); } }
URL input is: users/query?from=100&to=200&orderby=age&orderby=name
At this point, the output is: Getusers is called, from:100, to:200, Orderby[age, name]
Note that, unlike @pathparam, @queryparam specifies that the parameters in the URL appear as key-value pairs, while in the program
The @QueryParam ("from") int is read out of the value of the from in the URL,
In @pathparem, only the value of the parameter appears in the URL, no key-value pairs, such as: "/users/2011/06/30"
The
Java code
@GET @Path ("{year}/{month}/{day}") public Response getuserhistory ( @ Pathparam (int year , @PathParam (int month, @PathParam ( int Day ) { = year + '/' + month + '/' + day ; return Response.Status . Entity ("Getuserhistory is called, Year/month/day:" + date) . Build () ;
The output is:
Getuserhistory is called, YEAR/MONTH/DAY:2011/6/30
2 Get it in a dynamic way:
Java code
@Path ("/users") Public classUserService {@GET @Path ("/query") PublicResponse getusers (@Context uriinfo info) {String from= Info.getqueryparameters (). GetFirst ("from"); String to= Info.getqueryparameters (). GetFirst ("to"); List<String> = Info.getqueryparameters (). Get ("by"); returnResponse. Status (200). Entity (Getusers is called, from: ' + from + ', to: ' + to+ ", by" +orderby.tostring ()). build (); }
Url;users/query?from=100&to=200&orderby=age&orderby=name
The output is:
Getusers is called, from:100, to:200, Orderby[age, name]
Note that the two parameters after the order is read into the list processing.
3 @DefaultValue, default value
Example:
Java code
@Path ("/users") Public classUserService {@GET @Path ("/query") PublicResponse getusers (@DefaultValue ("" ") @QueryParam (" from ")intFrom , @DefaultValue ("999") @QueryParam ("to")intto, @DefaultValue ("Name") @QueryParam ("list<string>")) { returnResponse. Status (200). Entity (Getusers is called, from: ' + from + ', to: ' + to+ ", by" +orderby.tostring ()). build (); }
Url:users/query
Output: Getusers is called, from:1000, To:999,orderby[name]
That's a good understanding.
The difference between WebService @QueryParam @DefaultValue @PathParam