@javax. ws.rs WebService Annotation

Source: Internet
Author: User
Tags http post webservice annotation

For WebService. 1. Path

@javax. Ws.rs.Path The URI path that identifies the resource class or resource method to be requested.
Example, @Path ("animal") that represents the transaction to be processed when the next level path is animal.
@Path ("{species}") This notation with curly braces means that the next path is parameterized, with @pathparam ("species") using parameters that can be assigned to a function.

For code:

@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
	}
}

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

Assign the query parameter of the HTTP request to the parameter of the function.

@javax. Ws.rs.DefaultValue

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

public string Fun (@DefaultValue ("description") @QueryParam ("desc") string desc) {...}
@javax. ws.rs. Formparam

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

@avax. Ws.rs.Context

Used to get environment information. An example of obtaining a client IP is shown below:

@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
Indicates that this method responds to an HTTP GET request.

@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 up HTTP return message, Report style content type.
The value is Javax.ws.rs.core.MediaType.XXX. Commonly used are:

Mediatype.application_json
Mediatype.text_plain

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.