JAX-RS @ queryparam and @ defaultvalue

Source: Internet
Author: User

Let's take a look at @ queryparam
First look at the example:
 

Java code
  1. PATH ("/users ")
  2. Public class userservice {
  3. @ Get
  4. @ Path ("/query ")
  5. Public Response getusers (
  6. @ Queryparam ("from") int from,
  7. @ Queryparam ("to") int,
  8. @ Queryparam ("orderby") List <string> orderby ){
  9. Return response
  10. . Status (200)
  11. . Entity ("getusers is called, from:" + from + ", to:" +
  12. + ", Orderby" + orderby. tostring (). Build ();
  13. }
  14. }
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
  1. @ Get
  2. @ Path ("{year}/{month}/{day }")
  3. Public Response getuserhistory (
  4. @ Pathparam ("year") int year,
  5. @ Pathparam ("month") int month,
  6. @ Pathparam ("day") int day ){
  7. String date = year + "/" + month + "/" + Day;
  8. Return response. Status (200)
  9. . Entity ("getuserhistory is called, year/month/day:" + date)
  10. . Build ();
  11. }
@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
  1. @ Path ("/users ")
  2. Public class userservice {
  3. @ Get
  4. @ Path ("/query ")
  5. Public Response getusers (@ context uriinfo info ){
  6. String from = info. getqueryparameters (). getfirst ("from ");
  7. String to = info. getqueryparameters (). getfirst ("");
  8. List <string> orderby = info. getqueryparameters (). Get ("orderby ");
  9. Return response
  10. . Status (200)
  11. . Entity ("getusers is called, from:" + from + ", to:" +
  12. + ", Orderby" + orderby. tostring (). Build ();
  13. }
@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
  1. @ Path ("/users ")
  2. Public class userservice {
  3. @ Get
  4. @ Path ("/query ")
  5. Public Response getusers (
  6. @ Defaultvalue ("1000") @ queryparam ("from") int from,
  7. @ Defaultvalue ("999") @ queryparam ("to") int,
  8. @ Defaultvalue ("name") @ queryparam ("orderby") List <string> orderby ){
  9. Return response
  10. . Status (200)
  11. . Entity ("getusers is called, from:" + from + ", to:" +
  12. + ", Orderby" + orderby. tostring (). Build ();
  13. }
@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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.