Module 3: Product Service RESTful Web Service: Read Opeation - dineshmadhup/RESTful-Web-Service GitHub Wiki

Step: 1

First of all we have created Product model which has some features which is:

Product id => pid
Product name =>productName
Product Price =>productPrice
Date Created => created

In Java we have defined the Product features with the help of Constructors and and some methods - setters and getters as shown below:

Package: org.dinesh.softarica.productService.model

Product.java

@XmlRootElement
public class Product {

private long pid;
private String productName;
private Date created;
private String productPrice;

public Product() {
	
}

public Product(long pid, String productName, String productPrice) {
	this.pid = pid;
	this.productName = productName;
	this.productPrice = productPrice;
	this.created = new Date();
}

public long getPid() {
	return pid;
}

public void setPid(long pid) {
	this.pid = pid;
}

public String getProductName() {
	return productName;
}

public void setProductName(String productName) {
	this.productName = productName;
}

public Date getCreated() {
	return created;
}

public void setCreated(Date created) {
	this.created = created;
}

public String getProductPrice() {
	return productPrice;
}

public void setProductPrice(String productPrice) {
	this.productPrice = productPrice;
}

}

}

Here you notice the annotation we have used @XmlRootElement

The @XmlRootElement annotation can be used with the following program elements:
• a top level class
• an enum type

When a top level class or an enum type is annotated with the @XmlRootElement annotation, then its value is represented as XML element in an XML document.

Step: 2

Now the Product is needed to be stored in databases. Rather than creating database using MySQL or SQL we have used Map to store the products.

Package: org.dinesh.softarica.productService.database

DatabaseClass.java

public class DatabaseClass {

private static Map<Long, Product> myProducts = new HashMap<>();

public static Map<Long, Product>getProduct() {
	return myProducts;
}

}

Step: 3

Now we create a service where we define some methods so that we can store product details in DatabaseClass.

package: org.dinesh.softarica.productService.service;

ProductService.java

public class ProductService {

// Creating MAP with Key Long and value Product
private Map<Long, Product> myProducts = DatabaseClass.getProduct();

// No-arg constructor
public ProductService() {

	myProducts.put(1L, new Product(1, "IPhone7", "900"));
}

public List<Product> getAllProducts() {
	return new ArrayList<Product>(myProducts.values());
}

}

Step: 4

Creating resources

package: org.dinesh.softarica.productService.resources;

ProductResource.java

@Path("/products")
/* Adding Produces and Consumes annotation on class level /
/
do not require at each individual method */
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ProductResource {

ProductService productService = new ProductService();

@GET
//@Produces(MediaType.TEXT_PLAIN)
public List<Product> getProduct() {
	return productService.getAllProducts();
}

}

Result:

The annotation we have used is:
@Path(“/products”)

on the top level of the class named ProductResource which contains method getProduct and annotated with @GET

URL to check result:

http://localhost:8080/productService/webapi/products/

Output:

[ { "created": "2016-12-18T14:37:12.352", "pid": 1, "productName": "IPhone7", "productPrice": "900" } ]

Download: Source Code

⚠️ **GitHub.com Fallback** ⚠️