Restful Web Services Tutorial: What Is Rest Architecture?
Restful Web Services Tutorial: What Is Rest Architecture?
Audience
This tutorial is designed for Software Professionals who are willing to learn
RESTful Web Services in simple and easy steps. This tutorial will give you
great understanding on RESTful Web Services concepts and after
completing this tutorial you will be at intermediate level of expertise from
where you can take yourself at higher level of expertise.
Prerequisites
Before proceeding with this tutorial, you should have a basic
understanding of Java Language, Text Editor, etc. Because we are going
to develop web services applications using RESTful, so it will be good if
you have understanding on other web technologies like HTML, CSS, AJAX,
etc.
HTTP methods
Following four HTTP methods are commonly used in REST based
architecture.
If you are running Windows and installed the JDK in C:\jdk1.7.0_75, you
would have to put the following line in your C:\autoexec.bat file.
set PATH = C:\jdk1.7.0_75\bin;%PATH%
set JAVA_HOME = C:\jdk1.7.0_75
Make a choice whether you want to install Jersey on Windows, or Unix and
then proceed to the next step to download the .zip file for windows and then
the .tz file for Unix.
Download the latest version of Jersey framework binaries from the following
link – https://jersey.java.net/download.html.
or
C:\apache-tomcat-7.0.59\bin\startup.bat
or
/usr/local/apache-tomcat-7.0.59/bin/startup.sh
or
C:\apache-tomcat-7.0.59\bin\shutdown
or
/usr/local/apache-tomcat-7.0.59/bin/shutdown.sh
Once you are done with this last step, you are ready to proceed for your
first Jersey example which you will see in the next chapter.
So, let us proceed to write a simple Jersey Application which will expose a
web service method to display the list of users.
\jaxrs-ri-2.17\jaxrs-ri\api
\jaxrs-ri-2.17\jaxrs-ri\ext
\jaxrs-ri-2.17\jaxrs-ri\lib
Now, right click on your project name UserManagement and then follow
the option available in context menu − Build Path → Configure Build
Path to display the Java Build Path window.
Now use Add JARs button available under Libraries tab to add the JARs
present in WEBINF/lib directory.
Creating the Source Files
Now let us create the actual source files under
the UserManagement project. First we need to create a package
called com.tutorialspoint. To do this, right click on src in package
explorer section and follow the option − New → Package.
User.java
package com.tutorialspoint;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "user")
public User(){}
this.id = id;
this.name = name;
this.profession = profession;
return id;
}
@XmlElement
this.id = id;
return name;
@XmlElement
this.name = name;
return profession;
@XmlElement
this.profession = profession;
UserDao.java
package com.tutorialspoint;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
try {
if (!file.exists()) {
userList.add(user);
saveUserList(userList);
else{
ois.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
return userList;
try {
File file = new File("Users.dat");
FileOutputStream fos;
oos.writeObject(userList);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
UserService.java
package com.tutorialspoint;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/UserService")
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
There are two important points to be noted about the main program,
UserService.java
The first step is to specify a path for the web service using @Path annotation
to the UserService.
The second step is to specify a path for the particular web service method
using @Path annotation to method of UserService.
web.xml
xmlns = "http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
<display-name>User Management</display-name>
<servlet>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.tutorialspoint</param-value>
</init-param>
</servlet>
<servlet-mapping>
</servlet-mapping>
</web-app>
To create a WAR file using eclipse, follow the option File → export → Web
→ War File and finally select project UserManagement and destination
folder. To deploy a war file in Tomcat, place the UserManagement.war in
the Tomcat Installation Directory → webapps directory and start the
Tomcat.