Friday, May 6, 2016

Starting with: REST web services, server and client examples.

1) Open Eclipse IDE
2) Create dynamic web project named "startingWithRESTWebServiceServer" (and set 3 at dynamic module version)
3)Download from jersey download page last available JAX-RS 2.0 API jar distribution
4)Unzip it and copy all jars under api, ext and lib folders under WEB-INF/lib
5)Ensure that you have your "Web App Libraries" configured in your project build path.
6) right-click on src folder and create a new package name com.webservices
7)Then create User class with this content:

package com.webservices;

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 String name;
   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;
   }       
}


8)Then create UserDao class with this content:

package com.webservices;

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, "Maurizio", "Fisherman");
            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();
      }
   }  
}


9)Then create UserService class with this content:
10)Then create or update web.xml file(under WebContent folder) with this content:

<?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"
   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">
   <display-name>startingWithRESTWebServiceServer</display-name>
   <servlet>
      <servlet-name>REST Wer Service server example</servlet-name>
      <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
         <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.webservices</param-value>
         </init-param>
      </servlet>
   <servlet-mapping>
   <servlet-name>REST Wer Service server example</servlet-name>
      <url-pattern>/rest/*</url-pattern>
   </servlet-mapping> 
</web-app>




11)Now we are ready to deploy the application.
So deploy the exported project as war file under tomcat or configure it internal bundled and add the project to it.
Then we can use a chrome plugin extension(as SimpleRestClient) to test it.
Pointing to url:
http://localhost:8080/startingWithRESTWebServiceServer/rest/UserService/users

That's all....
Bye...
 

No comments:

Post a Comment