Hi Every one I hope you are read the previous post and understanding better towards we are going to database related restful webservices example for this you need to understand the how to use persistence bean class. If you know Hibernate or EJB is very easy even you don’t know also no problem for those people you need to see this post and see video so that you understand better and better way. The process of developing a REST web service, deploying it to the internal MyEclipse Tomcat server and testing it with the REST Web Services Explorer. I am showing step by step developing process.

1.    Creating the REST Web Service Project

                   To get started we will create a simple Web Service Project by selecting Web Service Project from the new toolbar menu or else in project pane you can click mouse right button and choose  à New à WebServiceProject. One popup menu kind of screen will appear in that you need to enter the following information.

Project Name: CustomerRestDemo(what ever name you can give no problem but follow standard naming convention is best practice).

Click Next to move to page 2 of the wizard. On this page you can specify the path at which the services will be available, the name of the corresponding JAX-RS servlet and libraries which you wish to add to your project. For this project the defaults are fine, so click Finish to create the project. Before clicking Finish make sure that libraries check box is selected or not generally first two check box’s are checked.

2.    Creating the REST Web Service

Here  in the process of creating the rest web service there is two following steps

1.    Creating the Customer entity
2.    Creating the CustomersResource class, the core of our web service

2.1 Creating the Customer entity:

                         To start, create a simple Customer class with id, name and address fields; this class represents the Customer entity we will be managing with our web service. Use the File > New > Class wizard, put Customer in the Name field, com.chandra.rest in the Package field and Finish the wizard. Replace the contents of the generated class with the following code

package com.chandra.rest;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Customer {

private int id;
private String name;
private String address;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

}
2.2 Creating the CustomersResource class, the core of our web service:

Select the (what ever project you created) project and invoke the new web service wizard from the toolbar.


After selecting New web Service one popup will propagate you need to fill upto these steps and further I will make a video for better understanding please see full video then only your able to understand next example actually I am giving code steps and process also
  1. generate the CustomersResource class, you will see a class with stubbed out resource methods as shown below:
package com.chandra.rest;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import com.sun.jersey.spi.resource.Singleton;

@Produces("application/xml")
@Path("customers")
@Singleton
public class CustomersResource {

@GET
public List<Customer> getCustomers() {
throw new UnsupportedOperationException("Not yet implemented.");
}

@GET
@Path("{id}")
public Customer getCustomer(@PathParam("id") int cId) {
throw new UnsupportedOperationException("Not yet implemented.");
}

@POST
@Path("add")
@Produces("text/plain")
@Consumes("application/xml")
public String addCustomer(Customer customer) {
throw new UnsupportedOperationException("Not yet implemented.");
}
}

Here we need to add method code that is showing in my video. please after uploading my video please visit and see





Post a Comment

 
Top