The difference between WebService @QueryParam @DefaultValue @PathParam

Source: Internet
Author: User

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

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.