Loading...
Sunday, September 25, 2016

Spring With Web Services Insert Product

                   Download


For Inserting Product Data.  Here  Using Postman chrome tool.

Step - 1  Select  POST Method

Step - 2  For Inserting Product Data Select Body.
In Body Chose row and Data Type is JSON(application/json)


ProductController.java
   



package java9r.controller.com;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java9r.entities.com.Product;
import java9r.service.com.ProductService;

@RestController
@RequestMapping("product")
public class ProductController {
@Autowired
ProductService productService;
  @RequestMapping(value = "/all/", method = RequestMethod.POST)
public ResponseEntity<Void> createProduct(@RequestBody Product product, UriComponentsBuilder ucBuilder) {
System.out.println("Creating product " + product.getName());

if (productService.isUserExist(product)) {
System.out.println("A Product with name " + product.getName() + " already exist");
return new ResponseEntity<Void>(HttpStatus.CONFLICT);
}

productService.insertProduct(product);

HttpHeaders headers = new HttpHeaders();
headers.setLocation(ucBuilder.path("/all/{id}").buildAndExpand(product.getId()).toUri());
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}

}






ProductService.java
   



package java9r.service.com;

import java.util.List;

import java9r.entities.com.Product;

public interface ProductService {

Product findByName(String name);

void insertProduct(Product product);
public boolean isUserExist(Product product);

}






ProductServiceImpl.java
   



package java9r.service.com;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java9r.dao.com.ProductDAO;
import java9r.entities.com.Product;

@Service("productService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ProductServiceImpl implements ProductService {
@Autowired
ProductDAO productDAO;

@Override
public Product findByName(String name) {

return productDAO.findByName(name);
}
@Override
public void insertProduct(Product product) {
productDAO.insertProduct(product);

}
@Override
public boolean isUserExist(Product product) {

return productDAO.isUserExist(product);
}

}






ProductDAO.java
   



package java9r.dao.com;

import java.util.*;

import java9r.entities.com.Product;

public interface ProductDAO {

Product findByName(String name);

void insertProduct(Product product);
public boolean isUserExist(Product product);

}






ProductDAOiImpl.java
   



package java9r.dao.com;

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

import org.springframework.stereotype.Repository;

import java9r.entities.com.Product;

@Repository
public class ProductDAOiImpl implements ProductDAO {

private static List<Product> products;

static {
products = productsData();
}


@Override
public Product findByName(String name) {
for(Product product : products){
if(product.getName().equalsIgnoreCase(name)){
return product;
}
}
return null;
}

@Override
public void insertProduct(Product product) {

products.add(product);

}


private static List<Product> productsData() {
List<Product> products = new ArrayList<Product>();
products.add(new Product(1, "product 1", 10, 10000, "product description 1", "product1.png"));
products.add(new Product(2, "product 2", 20, 20000, "product description 2", "product2.png"));
products.add(new Product(3, "product 3", 30, 30000, "product description 3", "product3.png"));
products.add(new Product(4, "product 4", 40, 40000, "product description 4", "product4.png"));
products.add(new Product(5, "product 5", 50, 50000, "product description 5", "product5.png"));
products.add(new Product(6, "product 6", 60, 60000, "product description 6", "product6.png"));
products.add(new Product(7, "product 7", 70, 70000, "product description 7", "product7.png"));
products.add(new Product(8, "product 8", 80, 80000, "product description 8", "product8.png"));

return products;
}

}






Product.java
   



package java9r.entities.com;

public class Product implements java.io.Serializable {

private static final long serialVersionUID = 6866588940548007313L;
private Integer id;
private String name;
private Integer price;
private Integer quantity;
private String description;
private String photo;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public Product(Integer id, String name, Integer price, Integer quantity, String description, String photo) {
super();
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
this.description = description;
this.photo = photo;
}
public Product() {
super();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((description == null) ? 0 : description.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((photo == null) ? 0 : photo.hashCode());
result = prime * result + ((price == null) ? 0 : price.hashCode());
result = prime * result + ((quantity == null) ? 0 : quantity.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Product other = (Product) obj;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (photo == null) {
if (other.photo != null)
return false;
} else if (!photo.equals(other.photo))
return false;
if (price == null) {
if (other.price != null)
return false;
} else if (!price.equals(other.price))
return false;
if (quantity == null) {
if (other.quantity != null)
return false;
} else if (!quantity.equals(other.quantity))
return false;
return true;
}
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", price=" + price + ", quantity=" + quantity + ", description="
+ description + ", photo=" + photo + "]";
}

}






web.xml
   



<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>SpringWithWebServicesInsertProduct</display-name>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>java9r</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>java9r</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


</web-app>






java9r-servlet.xml
   



<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


<context:component-scan base-package="java9r" />

<mvc:annotation-driven />
<mvc:default-servlet-handler />

</beans>





pom.xml
   



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>java9r</groupId>
<artifactId>SpringWithWebServicesInsertProduct</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>SpringWebServices</name>
<url>http://maven.apache.org</url>


<!-- Specifying the Versions of Spring, Hiberante, MySQL etc -->
<properties>
<spring.version>4.3.0.RELEASE</spring.version>
<hibernate.version>4.3.8.Final</hibernate.version>
<jackson.library>2.7.5</jackson.library>
<mysql.version>5.1.10</mysql.version>
<junit-version>4.11</junit-version>
<servlet-api-version>3.1.0</servlet-api-version>
<jsp-version>2.1</jsp-version>
<jstl-version>1.2</jstl-version>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<!-- Specifying the Dependencies -->
<dependencies>

<!-- Spring 4 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>



<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-version}</version>
<scope>provided</scope>
</dependency>


<!-- org.codehaus.jackson/jackson-mapper-asl -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.5.0</version>
</dependency>

<!-- javax.persistence/persistence-api -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>


<!-- commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.2</version>
</dependency>


<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<!-- artifact/org.slf4j/log4j-over-slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.6.6</version>
<scope>runtime</scope>
</dependency>
<!-- /artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>

<scope>compile</scope>
</dependency>

<!-- artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>

<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.library}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.library}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.library}</version>
</dependency>

<!-- artifact/org.javassist/javassist -->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
</dependency>


</dependencies>


<build>
<finalName>SpringWithWebServicesInsertProduct</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<configuration>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</execution>
</executions>
</plugin>


</plugins>
</build>
</project>






                          Download

0 comments:

Post a Comment

 
TOP