Introduction:
Restful web services is used for client server communication over the HTTP protocol.
For client server communication it uses many data format like Json, Xml, Html etc.
Now, How to create simple hello world example using restful web services in eclipse
please follow the given steps.
It Works on HTTP protocols.
1. GET for reading resources
2. PUT for creating resources
3. POST for updating resources
4. DELETE for deleting resources
We can define the paths to access particular resource by Annotations. Here some importanant annotations are there
1. @PATH - to define path
2. @POST - to handle HTTP POST request
3. @GET - to handle HTTP GET request
4. @PUT - to handle HTTP PUT request
5. @DELETE - to handle HTTP DELETE request
6. @Produces - to define output type
7. @Consumes - to define input
8. @PathParam - to get parameter values in post request.
9. @QueryParam - to get parameter values in get request (in url)
Requirements:
1. Eclipse or netbeans
2. Tomcat
3. jdk 1.5 or above
4. Following libraries for rest:
jersey-client-1.17.jar
jersey-core-1.17.jar
jersey-server-1.17.jar
jersey-servlet-1.17.jar
xstream-1.17.jar
Download these jar from here
Step1:
Open Eclipse > File > New > Dynamic Web Project > 'enter project name'
Step2:
Configure all libraries into the build path Or right click on project select 'properties' and click on
'java build path' then click on 'Add External Jar' then locate your libraries folder and select all above mentioned jars and finally click on OK.
Step3:
Now create index.html file inside web content with following contents:
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Rest Examples</title>
</head>
<body>
<a href="http://localhost:8080/RestWebservices/rs/test/hello>Click Here</a>
</body>
</html>
Step4:
Now create HelloWorldRest.java for testing with following contents:
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("/test")
public class HelloWorldRest {
@GET
@Path("/hello")
public String hello()
{
String hello="Welcome To Restfull Web Services";
System.out.println(hello);
return hello;
}
}
Here we are using some annotation like
@Path-this annotation is used for identify the path of class
@GET- this is http GET method for calling web services.
Step5:
Now create the web.xml file inside WEB-INF folder with following contents
<?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>CaseStudyREST</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/rs/*</url-pattern>
</servlet-mapping>
</web-app>
Here we are using ServletAdaptor for handling web services
Step6: RUN APPLICATION
Now start server and right click on project goto run>run as server
Now you will be able to see output in browser click on 'CLICK HERE'
Thanks for reading this tutorial hope it will help you.