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?
- @Path ("Animal")
- Public class Animal {
- Public String Species,name;
- public int age;
- public static Animal animal=new Animal ();
- @GET
- @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;
- }
- }
@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?
- 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?
- @Path ("util")
- Public class Util {
- @Path ("Getclientip")
- @GET
- @Produces (Mediatype.text_plain)
- Public String Getclientip (@Context httpservletrequest request) {
- return request.getremoteaddr ();
- }
- }
@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