Module 5: Product Service RESTful Web Service: Update Operation - dineshmadhup/RESTful-Web-Service GitHub Wiki
In this Module there is only change in code in STEP-3 (ProductService.java) and STEP-4 (ProductResource.java) compare to Module-2.
In this demonstration we show you three operation which is: Create, Read and Update
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.
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;
}
}
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());
}
// Create operation
public Product addProduct(Product product) {
product.setPid(myProducts.size() + 1);
myProducts.put(product.getPid(), product);
return product;
}
// Update Product
public Product updateProduct(Product product) {
if(product.getPid() <= 0) {
return null;
}
myProducts.put(product.getPid(), product);
return product;
}
}
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
public List<Product> getProduct() {
return productService.getAllProducts();
}
@POST
public Product addProduct(Product product) {
return productService.addProduct(product);
}
@PUT
@Path("/{productId}")
public Product updateProduct(@PathParam("productId") long pid, Product product) {
product.setPid(pid);
return productService.updateProduct(product);
}
}
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
http://localhost:8080/productService/webapi/products/
By choosing GET operation see the list of products:
By choosing POST operation we add the product then perform the Update:
Total List of Products which we have added so far:
Lets Update the product whose pid is 3, product name is car and price 80,000 . We update name Hybrid car and price 100,000.
Final List after update:
Download: Source Code