A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Simple Example of RestFul Webservice in Java

Rest is an architectural design/style for webservice application. JAX-RS is an java api based on rest webservice. There is Jersey and other jars provided by tomcat application. 

Let's checkout an example that gives json data, when a user try to hit below url which is aa below: 

Requested Url to fetch all publisher:

Output will be as below:
[Publisher [pid=101, pname=Navneet Kaur]][Publisher [pid=102, pname=Heera Babu]]

This is the sample example of restful web services that will give you a good idea. Further, you will be capable to work on rest service of java.


I’ve created Maven project with artifacts as webapp 1.0 which has Project structure look like as below:




Here, I’ve used jars as below:




Step 1. First create POJO class Publisher as below:
package com.Dream.rest;

public class Publisher {
       int pid;
       String pname;
      
       //setter & getter
      
       public int getPid() {
              return pid;
       }
       public void setPid(int pid) {
              this.pid = pid;
       }
       public String getPname() {
              return pname;
       }
       public void setPname(String pname) {
              this.pname = pname;
       }
       public Publisher(int pid, String pname) {
              this.pid = pid;
              this.pname = pname;
       }
       @Override
       public String toString() {
              return "Publisher [pid=" + pid + ", pname=" + pname + "]";
       }

}







Step 2. Then, create DATA Access class PublisherDao as below:
package com.Dream.rest;

import java.util.ArrayList;
import java.util.List;

public class PublisherDao {
      
       List<Publisher> plist=null;

       //insert
       public void savePublishers(Publisher p)
       {
              plist=new ArrayList<Publisher>();
              plist.add(p);
              System.out.println("inserted....");
       }
      
       //retrieve
       public List<Publisher> retrievePublishers()
       {
                     System.out.println("retrieving....");
                     return plist;
       }
}

Step 3. Then, create webservice Controller class PublisherController as below:
package com.Dream.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/publisher")
public class PublisherController {

      
              // This method is called if HTML and XML is not requested 
                @GET 
                @Produces(MediaType.TEXT_PLAIN
                public String displayPublishers()
                {
                       Publisher p1 = new Publisher(101,"Navneet Kaur");
                       Publisher p2 = new Publisher(102,"Heera Babu");
                      
                       PublisherDao pdao=new PublisherDao();
                       pdao.savePublishers(p1);
                       String v1=String.valueOf(pdao.retrievePublishers());

                       pdao.savePublishers(p2);
                       String v2=String.valueOf(pdao.retrievePublishers());
                       String mylist=v1+v2;
                       return mylist
                }
}

Step 4. Reconfig server congiguration xml file web.xml as 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"
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">

<servlet>
    <servlet-name>Jersey REST Service</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.Dream.rest</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>




LEARN TUTORIALS

.

.