How to create a Restful Web service?
This example help you all to understand the basics of Restful Webservice creation without any pluginYou need to do the following things to do
1)Create a Dynamic Web Project
2)Put the following jar files into the WEB-INF/lib folder
asm-x.x.jar
jersey-core-x.x.jar
jersey-json-x.x.jar
jersey-server-x.x.jar
jersey-servlet-x.x.jar
jersey-bundle-x.x.jar
jsr311-api.jar
3)Configure the web.xml.The code is given below
<?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/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Rest WS Test</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
4)Write a Class which extends the Application.This class we need to override the classes which need to publish the web service methods
package com.test;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class TestApplication extends Application{
@Override
public Set<Class<?>> getClasses(){
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(TestRestWebService.class);
return classes;
}
}
5)Web service classes in which we actually define the web methods(Ideompotent methods) like GET,POST,PUT,DELETE etc.
package com.test;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
@Path("/testrs")
public class TestRestWebService {
@GET
public String getInfo(@QueryParam("name") String name) {
Date dt=new Date();
SimpleDateFormat sdf=new SimpleDateFormat();
String str="Hello Mr "+ name+" Today is "+ dt.getDate() +"-"+dt.getMonth()+"-"+(Integer.valueOf(dt.getYear())+1900);
return str;
}
@POST
public String postInfo(@QueryParam("name") String name) {
Date dt=new Date();
SimpleDateFormat sdf=new SimpleDateFormat();
String str="Hello Mr post "+ name+" Today is "+ dt.getDate() +"-"+dt.getMonth()+"-"+(Integer.valueOf(dt.getYear())+1900);
return str;
}
@PUT
public String putInfo(@QueryParam("name") String name) {
Date dt=new Date();
SimpleDateFormat sdf=new SimpleDateFormat();
String str="Hello Mr put "+ name+" Today is "+ dt.getDate() +"-"+dt.getMonth()+"-"+(Integer.valueOf(dt.getYear())+1900);
return str;
}
}
6)Create war file and deploy it in server.
I assume you are using a tomcat server then
your request for rest call looks like
http://localhost:8080/<ProjectName>/rest/testrs?name=xxxx