1. Eclipse Create a new Dynamic Web project
2. Download the Jersey library file, https://jersey.java.net/, and copy all of the jars to the Webcontent/web-inf/lib directory (currently the latest version is Jaxrs-ri-2.9.zip)
3. Ensure that the current use is jre7/jdk1.7 to compatible Jersey class library
4. Write HelloWorld Web Service
Package Cn.com.chaser.restapi;
Import Org.glassfish.jersey.filter.LoggingFilter;
Import Org.glassfish.jersey.server.ResourceConfig;
public class Restapplication extends ResourceConfig {public
restapplication () {
packages () Cn.com.chaser.restapi ");
Register (loggingfilter.class);
}
Create a rest Api
Package Cn.com.chaser.restapi;
Import Javax.ws.rs.DefaultValue;
Import Javax.ws.rs.GET;
Import Javax.ws.rs.Path;
Import Javax.ws.rs.PathParam;
Import javax.ws.rs.Produces;
Import Javax.ws.rs.QueryParam;
Import Javax.ws.rs.core.MediaType;
Import Javax.ws.rs.core.Response;
@Path ("/restservice") public
class Restservice {
@GET
@Path ("/getinfo")
@Produces ( Mediatype.text_plain) Public
String Getwebserviceinfo () {return
"hello,restful web service!";
}
@GET
@Path ("/{parameter}") Public
Response respondmessage (@PathParam ("parameter") String parameter,
@DefaultValue ("Nothing to Say") @QueryParam ("value") String value) {
String out = ' Hello from ' + parameter + ': ' + value;
return Response.Status. Entity (out). Build ();
@GET: Indicates that the interface accepts a GET method
@Path: Defining Access paths
@Produces (Mediatype.text_plain)
Return text information
5. Preparation of Webcontent/web-inf/web.xml
<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemalocation= "http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id= "Webapp_ ID "version=" 3.0 > <display-name>RestService</display-name> <servlet> <servlet-name> Mobile</servlet-name> <servlet-class>org.glassfish.jersey.servlet.servletcontainer </ servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value >cn.com.chaser.restapi.RestApplication</param-value> </init-param> <load-on-startup>1</ load-on-startup> </servlet> <servlet-mapping> <servlet-name>mobile</servlet-name> < Url-pattern>/rest/*</url-pattern> </servlet-mapping> <welcome-file-liSt> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> < Welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </ Welcome-file-list> </web-app>
6. Right click on item, Export to war file, to tomcat7/webapps/directory
7. Start TOMCAT7 for testing
A) Http://localhost:8080/RestService/rest/restService/getInfo
Show:
Hello,restful Web service!
b) http://localhost:8080/RestService/rest/restService/getInfow
Show:
Hello from getinfow:nothing to say
c) http://localhost:8080/RestService/rest/restService/wwwsw?value=24
Return:
Hello from Wwwsw:24
Reference:
1. http://www.vogella.com/tutorials/REST/article.html
2. https://jersey.java.net/nonav/documentation/latest/user-guide.html