Java Libraries
Use JAR files which are listed below:
antlr-2.7.7.jar
asm-3.1.jar
commons-logging-1.1.3.jar
dom4j-1.6.1.jar
genson-1.3.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.5.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
jackson-core-asl-1.9.2.jar
jackson-jaxrs-1.9.2.jar
jackson-mapper-asl-1.9.2.jar
jackson-xc-1.9.2.jar
javassist-3.15.0-GA.jar
javax.servlet.jsp.jstl-1.2.2.jar
javax.servlet.jsp.jstl-api-1.2.1.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
jersey-client-1.18.jar
jersey-core-1.18.jar
jersey-json-1.19.3.jar
jersey-server-1.18.jar
jersey-servlet-1.18.jar
jettison-1.1.jar
jsr311-api-1.1.1.jar
mysql-connector-java-5.1.36.jar
taglibs-standard-impl-1.2.1.jar
Create Server Project
Create Dynamic Web Project in Eclipse. Copy all jar files above to the lib directory in the project
Create ApplicationConfig Class
This class is used to register a URL pattern in Jersey to intercept HTTP calls to the service. The marshaller, Jersey intercepts the path given in @ApplicationPath annotation such as – @ApplicationPath(“api”). This class extends javax.ws.rs.core.Application. All the RESTful web services and extensions needed are registered here.
ApplicationConfig.java
package ws;
import java.util.Set;
import javax.ws.rs.core.Application;
@javax.ws.rs.ApplicationPath("api")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
addRestResourceClasses(resources);
return resources;
}
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(controllers.DemoRestController.class);
}
}
Entity Class
Create entities package in server project. Create a entity class: Product.java as below
Product.java
package entities;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "product")
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String name;
private double price;
@XmlElement(name = "id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement(name = "price")
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Product(String id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public Product() {
}
}
Create Restful Web Services
Create Restful Web Services provides application/json data for the client
package controllers;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import entities.Product;
@Path("demo")
public class DemoRestController {
@GET
@Path("find")
@Produces({ MediaType.APPLICATION_JSON })
public Response find() {
try {
List<Product> products = new ArrayList<Product>();
products.add(new Product("p01", "name 1", 100));
products.add(new Product("p02", "name 2", 200));
products.add(new Product("p03", "name 3", 300));
GenericEntity<List<Product>> genericEntity = new GenericEntity<List<Product>>(products){};
return Response.ok(genericEntity, MediaType.APPLICATION_JSON).build();
} catch (Exception e) {
return Response.status(Response.Status.BAD_REQUEST).build();
}
}
}
Testing Restful Web Services
Access restful web services use the following url: http://localhost:8080/LearnJavaRestfulWebServices_Server/api/demo/find
Output
[
{"id":"p01","name":"name 1","price":100.0},
{"id":"p02","name":"name 2","price":200.0},
{"id":"p03","name":"name 3","price":300.0}
]
Consume Restful Web Services from Java Application
Create Java Project in Eclipse. Add all jar files above to the project
Entity Class
Create entities package in client project. Create a entity class: Product.java as below
Product.java
package entities;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "product")
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String name;
private double price;
@XmlElement(name = "id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement(name = "price")
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Product(String id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public Product() {
}
}
Create DemoRestClientModel
DemoRestClientModel class contain methods call restful web services
package models;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.DefaultClientConfig;
public class DemoRestClientModel {
private String BASE_URL = "http://localhost:8080/LearnJavaRestfulWebServices_Server/api/";
private WebResource webResource;
private Client client;
public DemoRestClientModel() {
this.client = Client.create(new DefaultClientConfig());
this.webResource = this.client.resource(BASE_URL).path("demo");
}
public ClientResponse find() {
ClientResponse response = null;
try {
WebResource resource = this.webResource;
response = resource.path("find")
.accept(MediaType.APPLICATION_JSON)
.get(ClientResponse.class);
} catch (Exception e) {
response = null;
}
return response;
}
}
Run It
package demo;
import java.util.List;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import entities.Product;
import models.DemoRestClientModel;
public class Main {
public static void main(String[] args) {
DemoRestClientModel demoRestClientModel = new DemoRestClientModel();
ClientResponse response = demoRestClientModel.find();
if(response != null) {
int statusCode = response.getStatus();
System.out.println("Response Status: " + statusCode);
GenericType<<Product>> genericType = new GenericType<List<Product>>(){};
List<Product> products = response.getEntity(genericType);
System.out.println("\nProduct List");
for(Product product : products) {
System.out.println("Id: " + product.getId());
System.out.println("Name: " + product.getName());
System.out.println("Price: " + product.getPrice());
System.out.println("==========================");
}
} else {
System.err.println("Can not access find web method");
}
}
}
Output
Response Status: 200
Product List
Id: p01
Name: name 1
Price: 100.0
==========================
Id: p02
Name: name 2
Price: 200.0
==========================
Id: p03
Name: name 3
Price: 300.0
==========================