TEXTBOOKS RESTful Web Services - First Application

Let's start writing real RESTful web services with the Jersey Framework. Before you start writing your first example using the Jersey Framework, you should ensure that you have set up the Jersey environment correctly, as described in the RESTful Web Services - Environment Environment Setup chapter . Here I also assume that you are somewhat familiar with the Eclipse IDE.
So let's get started writing a simple Jersey application that will provide a web service method to display a list of users.
Creating a Java project
The first step is to create a dynamic web project using the Eclipse IDE. Select File→New→Project and finally select the Dynamic Web Project wizard from the list. Now name your project as UserManagement using wizard window as shown in the following screenshot −
Once your project is successfully created, you will see the following content in Project Explorer −
Adding Required Libraries
As a second step, let's add the Jersey Framework and its dependencies (libraries) to our project. Copy all jars from the following directories of the jersey boot zip folder to the WEB-INF/lib directory of the project.
- \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 UserManagement project name and select the option available in the context menu - Build Path → Configure Build Path to display the Java Build Path window.
Now use the Add JAR button available in the Libraries tab to add the JARs present in the WEBINF/lib directory.
Creating Source Files
Now let's create the actual source files in the UserManagement project . First we need to create a package named com.tutorialspoint . To do this, right-click on src in the package explorer section and select the option - New → Package .
Next, we will create UserService.java, User.java, UserDao.java files in the com.tutorialspoint 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 class User implements Serializable { private static final long serialVersionUID = 1L ; private int id ; private Stringname ; _ private String profession ; public User (){} public User ( int id , String name , String profession ){ this . id = id ; this . name = name ; this . profession = profession ; } public int getId () { return id ; } @XmlElement public void setId ( int id ) { this . id = id ; } public String getName () { return name ; } @XmlElement public void setName ( String name ) { this . name = name ; } public String getProfession () { return profession ; } @XmlElement public void setProfession ( String profession ) { 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 ; public class UserDao { public List < User > getAllUsers (){ List < User > userList = null ; try { File file = new File ( "Users.dat" ); if (! file . exists ()) { User user = new User ( 1 , "Mahesh" , "Teacher" ); userList = new ArrayList < User >(); userList . add ( user ); saveUserList ( userList ); } else { FileInputStream fis = new FileInputStream ( file ); ObjectInputStream ois = new ObjectInputStream ( fis ); userList = ( List < User >) ois . readObject (); ois . close (); } } catch ( IOException e ) { e . printStackTrace (); } catch ( ClassNotFoundException e ) { e . printStackTrace (); } return userList ; } private void saveUserList ( List < User > userList ) { try { File file = new File ( "Users.dat" ); FileOutputStream fos ; fos = new FileOutputStream ( file ); ObjectOutputStream oos = new ObjectOutputStream ( 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" ) public class UserService { UserDao userDao = new UserDao (); @GET @Path ( "/users" ) @Produces ( MediaType . APPLICATION_XML ) public List < User > getUsers (){ return userDao . getAllUsers (); } }
There are two important points to note about the main program:
UserService.java
-
The first step is to specify the path for the web service using the @Path annotation on the UserService.
-
The second step is to specify the path for the specific web service method using the @Path annotation on the UserService method.
The first step is to specify the path for the web service using the @Path annotation on the UserService.
The second step is to specify the path for the specific web service method using the @Path annotation on the UserService method.
Creating a Web.xml Configuration File
You need to create a Web xml configuration file, which is an XML file and is used to specify the Jersey platform servlet for our application.
web.xml
<? xml version = "1.0" encoding = "UTF-8" ?>xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 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" id = "WebApp_ID" version = "3.0" >User Management < servlet-name> Jersey RESTful Application org.glassfish.jersey.servlet.ServletContainer jersey.config.server.provider .packages com.tutorialspoint Jersey RESTful Application /rest/*
Program Deployment
When you've finished creating the source and web configuration files, you'll be ready for this step, which compiles and runs your program. To do this, using Eclipse, export your application as a war file and deploy it to tomcat.
To create a WAR file using Eclipse, choose File → Export → Web → War File and finally select the project's UserManagement and destination folder. To deploy the war file to Tomcat, place the UserManagement.war file in the Tomcat installation directory → webapps directory and start Tomcat.
Program launch
We use Postman , a Chrome extension, to test our web services.
Make a request to UserManagement to get a list of all users. Put http://localhost:8080/UserManagement/rest/UserService/users in a POSTMAN with a GET request and see the following result.
Congratulations, you have successfully created your first RESTful application.