"Go" @javax. Ws.rs WebService Annotations

Source: Internet
Author: User
Tags http post

For WebService.

1. Path

@javax. ws.rs. Path

Identifies the URI path of the resource class or resource method to be requested.
Example, @Path ("Animal"), which represents the transaction to be processed when the next layer path is animal.
@Path ("{species}") This representation of curly braces, indicating that the next layer of path is parameterized, with @pathparam ("species") using parameters that can be assigned to the function.

For code:

[Java]View Plaincopy print?
  1. @Path ("Animal")
  2. Public class Animal {
  3. Public String Species,name;
  4. public int age;
  5. public static Animal animal=new Animal ();
  6. @GET
  7. @Path ("{species}")
  8. @Produces (Mediatype.application_json)
  9. Public Animal Wsanimal (@PathParam ("species") String species,
  10. @QueryParam ("name") String name,
  11. @QueryParam ("age") int Age
  12. ){
  13. Animal.species=species;
  14. Animal.name=name;
  15. Animal.age= age==0?   2:age;
  16. return animal;
  17. }
  18. }
@Path ("Animal") public class Animal {public String species,name;public int age;public static animal animal=new animal (); @G Et@path ("{species}") @Produces (Mediatype.application_json) public Animal wsanimal (@PathParam ("species") String Species, @QueryParam ("name") String name, @QueryParam ("age") int age) {animal.species=species;animal.name=name; animal.age=  age==0?2:age;return Animal;}}


The effect is shown in Figure 1-1:

Figure 1-1 @Path Usage Example

2. From message data to method parameters

@javax. ws.rs. Pathparam

Binds the path parameter specified in the URI to the resource method parameter, the field of the resource class, or the Bean property of the resource class.

@javax. ws.rs. Queryparam

Assigns the query parameter of the HTTP request to the parameters of the function.

@javax. Ws.rs.DefaultValue

Sets the default value for the @queryparam parameter. If the @queryparam does not receive a value, the default value is used. Like what:

[Java]View Plaincopy print?
    1. public string Fun (@DefaultValue ("description") @QueryParam ("desc") String desc) {...}
public string Fun (@DefaultValue ("description") @QueryParam ("desc") String desc) {...}

@javax. ws.rs. Formparam

Assigns the parameters in the form form of the HTTP request to the parameters of the function.

@avax. ws.rs. Context

Used to obtain environmental information. An example of obtaining a client IP is shown below:

[Java]View Plaincopy print?
  1. @Path ("util")
  2. Public class Util {
  3. @Path ("Getclientip")
  4. @GET
  5. @Produces (Mediatype.text_plain)
  6. Public String Getclientip (@Context httpservletrequest request) {
  7. return request.getremoteaddr ();
  8. }
  9. }
@Path ("Util") public class Util {@Path ("Getclientip") @GET @produces (mediatype.text_plain) public String Getclientip (@ Context HttpServletRequest request) {return request.getremoteaddr ();}}

3.http method

@javax. ws.rs. GET
Represents an HTTP GET request that this method responds to.

@javax. ws.rs. POST
Indicates that this method responds to an HTTP POST request.

@javax. ws.rs. PUT

Typically used to update data.

@javax. ws.rs. DELETE

Typically used to delete data.

4. Return Data type

@javax. ws.rs. produces
Set the HTTP return message to report the stylistic content type.
The value is Javax.ws.rs.core.MediaType.XXX. Commonly used are:

MediaType. Application_json
MediaType. Text_plain

"Go" @javax. Ws.rs WebService Annotations

Related Article

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.