Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Thursday, 14 August 2014

Spring 4 Restful Web Services With Hibernate 4 Example using Maven – Server Application

Spring 4 Restful Web Services With Hibernate 4 Example using Maven – Server Application In our previous discussions we came across several Spring related topics. In this particular blog we will see how to create Spring Restful Web Services With Hibernate using Maven. We will create and build the project with Maven and hibernate 4 will be used in data layer.

To get started simply create a simple web application with maven and import it in Eclipse. If you are new to Maven please go through the Link below.

More Details on : Dynamic Web Project with Maven and Import it in eclipse

Now we have a skeleton Maven project imported to eclipse, lets add all required Java and Configuration files to it as shown in the rest part of the tutorial.

Database Setup
Here us MySql script to create a simple database and table structure to be used in our application, simply paste the script in query editor and run to make a DB skeleton.
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET FOREIGN_KEY_CHECKS=0 */;

-- Dumping database structure for employee_db
CREATE DATABASE IF NOT EXISTS `employee_db` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `employee_db`;


-- Dumping structure for table employee_db.employee
CREATE TABLE IF NOT EXISTS `employee` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(45) DEFAULT NULL,
  `last_name` varchar(45) DEFAULT NULL,
  `email` varchar(45) DEFAULT NULL,
  `phone` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

-- Dumping data for table employee_db.employee: ~1 rows (approximately)
/*!40000 ALTER TABLE `employee` DISABLE KEYS */;
INSERT INTO `employee` (`id`, `first_name`, `last_name`, `email`, `phone`) VALUES
 (2, 'Hoston', 'lindey', 'hl@gmail.com', '90908989899');
/*!40000 ALTER TABLE `employee` ENABLE KEYS */;
/*!40014 SET FOREIGN_KEY_CHECKS=1 */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;



Dependencies required to make Spring Restful Web Services with Hibernate

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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.beingjavaguys.sample</groupId>
 <artifactId>SpringRestCrud</artifactId>
 <packaging>war</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>SpringRestCrud Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <properties>
  <spring.version>4.0.5.RELEASE</spring.version>
  <hibernate.version>4.3.5.Final</hibernate.version>
  <log4j.version>1.2.17</log4j.version>
  <jdk.version>1.7</jdk.version>
  <context.path>SpringRestCrud</context.path>
 </properties>
 <build>
  <finalName>${pom.artifactId}</finalName>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>${jdk.version}</source>
     <target>${jdk.version}</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <dependencies>
  <dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>${log4j.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-orm</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</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>jstl</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
  </dependency>
  <dependency>
   <groupId>com.google.code.gson</groupId>
   <artifactId>gson</artifactId>
   <version>2.2.2</version>
  </dependency>
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.6</version>
  </dependency>
  <dependency>
   <groupId>org.codehaus.jackson</groupId>
   <artifactId>jackson-mapper-asl</artifactId>
   <version>1.9.10</version>
  </dependency>
  <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-entitymanager</artifactId>
   <version>${hibernate.version}</version>
  </dependency>
 </dependencies>
</project>



\src\main\webapp\WEB-INF\web.xml
We need to make an Spring entry to web.xml, this will tell the container that all upcoming requests will be served by Spring Framework itself as per the configuration.
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 version="2.5">

 <display-name>Sample Spring Maven Project</display-name>

 <servlet>
  <servlet-name>mvc-dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/spring-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>mvc-dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>

</web-app>


\src\main\webapp\WEB-INF\spring-config.xml
This is simple spring configuration file, we have added entry for base-package her to search and find Controller classes with annotations. We defined two beans here for Service and Dao layer that we will see in details shortly. Rest is hibernate configurations and a datasource bean to connect with database. More details on : Spring Hibernate Integration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

 <context:component-scan base-package="com.beingjavaguys.controller" />
 <mvc:annotation-driven />

 <bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="jdbc:mysql://localhost:3306/employee_db" />
  <property name="username" value="root" />
  <property name="password" value="root" />
 </bean>

 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="annotatedClasses">
   <list>
    <value>com.beingjavaguys.model.Employee</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
   </props>
  </property>
 </bean>

 <bean id="txManager"
  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>

 <bean id="persistenceExceptionTranslationPostProcessor"
  class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

 <bean id="dataDao" class="com.beingjavaguys.dao.DataDaoImpl"></bean>
 <bean id="dataServices" class="com.beingjavaguys.services.DataServicesImpl"></bean>
</beans>

\src\main\java\com\beingjavaguys\controller\RestController.java
This is simple Spring MVC controller with annotations, we have added all general purpose methods here those method will accept a rest request in Json form and will return a JSON response. The methods are self explanatory we have used @Controller annotation to point incoming requests to this class, and @RequestMapping annotation to point incoming requests to appropriate Methods. @RequestBody annotation is used to accept data with request in Json form and @ResponseBody is used to return JSON as response to incoming request.
package com.beingjavaguys.controller;

import java.util.List;

import org.apache.log4j.Logger;
import com.beingjavaguys.model.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.beingjavaguys.model.Employee;
import com.beingjavaguys.services.DataServices;

@Controller
@RequestMapping("/employee")
public class RestController {

 @Autowired
 DataServices dataServices;

 static final Logger logger = Logger.getLogger(RestController.class);

 /* Submit form in Spring Restful Services */
 @RequestMapping(value = "/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
 public @ResponseBody
 Status addEmployee(@RequestBody Employee employee) {
  try {
   dataServices.addEntity(employee);
   return new Status(1, "Employee added Successfully !");
  } catch (Exception e) {
   // e.printStackTrace();
   return new Status(0, e.toString());
  }

 }

 /* Ger a single objct in Json form in Spring Rest Services */
 @RequestMapping(value = "/{id}", method = RequestMethod.GET)
 public @ResponseBody
 Employee getEmployee(@PathVariable("id") long id) {
  Employee employee = null;
  try {
   employee = dataServices.getEntityById(id);

  } catch (Exception e) {
   e.printStackTrace();
  }
  return employee;
 }

 /* Getting List of objects in Json format in Spring Restful Services */
 @RequestMapping(value = "/list", method = RequestMethod.GET)
 public @ResponseBody
 List getEmployee() {

  List employeeList = null;
  try {
   employeeList = dataServices.getEntityList();

  } catch (Exception e) {
   e.printStackTrace();
  }

  return employeeList;
 }

 /* Delete an object from DB in Spring Restful Services */
 @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
 public @ResponseBody
 Status deleteEmployee(@PathVariable("id") long id) {

  try {
   dataServices.deleteEntity(id);
   return new Status(1, "Employee deleted Successfully !");
  } catch (Exception e) {
   return new Status(0, e.toString());
  }

 }
}


More details on : Spring JSON – Output Json in Spring application

Model Classes

\SpringRestCrud\src\main\java\com\beingjavaguys\model\Employee.java
This is simple POJO to represent Employee entity in our application, we will be dealing with Employee entity to save, retrieve and delete data using Spring Restful Web Services. We have annotated the class with Hibernate annotations to make hibernate aware of the entity.
package com.beingjavaguys.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

@Entity
@Table(name = "employee")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Employee implements Serializable {

 private static final long serialVersionUID = 1L;

 @Id
 @GeneratedValue
 @Column(name = "id")
 private long id;

 @Column(name = "first_name")
 private String firstName;

 @Column(name = "last_name")
 private String lastName;

 @Column(name = "email")
 private String email;

 @Column(name = "phone")
 private String phone;

 public long getId() {
  return id;
 }

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

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getEmail() {
  return email;
 }

 public void setEmail(String email) {
  this.email = email;
 }

 public String getPhone() {
  return phone;
 }

 public void setPhone(String phone) {
  this.phone = phone;
 }
}



Don’t forget to add @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) annotation to class, this is used to resolve Hibernate and Json conflict.

\src\main\java\com\beingjavaguys\model\Status.java
This is another POJO to represent conditional Status and Error messages in form of Json, this comes handy in case of sending status response to requests if needed.
package com.beingjavaguys.model;

public class Status {

 private int code;
 private String message;

 public Status() {
 }

 public Status(int code, String message) {
  this.code = code;
  this.message = message;
 }

 public int getCode() {
  return code;
 }

 public void setCode(int code) {
  this.code = code;
 }

 public String getMessage() {
  return message;
 }

 public void setMessage(String message) {
  this.message = message;
 }
}



Data layer with spring and hibernate

Below are simple Service and Dao classes and Interfaces to make a two tier data layer between Spring and Hibernate.

More details on : Spring Hibernate Integration

\src\main\java\com\beingjavaguys\dao\DataDao.java
package com.beingjavaguys.dao;

import java.util.List;

import com.beingjavaguys.model.Employee;

public interface DataDao {

 public boolean addEntity(Employee employee) throws Exception;
 public Employee getEntityById(long id) throws Exception;
 public List getEntityList() throws Exception;
 public boolean deleteEntity(long id) throws Exception;
}



\src\main\java\com\beingjavaguys\dao\DataDaoImpl.java
package com.beingjavaguys.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import com.beingjavaguys.model.Employee;

public class DataDaoImpl implements DataDao {

 @Autowired
 SessionFactory sessionFactory;

 Session session = null;
 Transaction tx = null;

 @Override
 public boolean addEntity(Employee employee) throws Exception {

  session = sessionFactory.openSession();
  tx = session.beginTransaction();
  session.save(employee);
  tx.commit();
  session.close();

  return false;
 }

 @Override
 public Employee getEntityById(long id) throws Exception {
  session = sessionFactory.openSession();
  Employee employee = (Employee) session.load(Employee.class,
    new Long(id));
  tx = session.getTransaction();
  session.beginTransaction();
  tx.commit();
  return employee;
 }

 @SuppressWarnings("unchecked")
 @Override
 public List getEntityList() throws Exception {
  session = sessionFactory.openSession();
  tx = session.beginTransaction();
  List employeeList = session.createCriteria(Employee.class)
    .list();
  tx.commit();
  session.close();
  return employeeList;
 }
 
 @Override
 public boolean deleteEntity(long id)
   throws Exception {
  session = sessionFactory.openSession();
  Object o = session.load(Employee.class, id);
  tx = session.getTransaction();
  session.beginTransaction();
  session.delete(o);
  tx.commit();
  return false;
 }

}



\src\main\java\com\beingjavaguys\services\DataServices.java
package com.beingjavaguys.services;

import java.util.List;

import com.beingjavaguys.model.Employee;

public interface DataServices {
 public boolean addEntity(Employee employee) throws Exception;
 public Employee getEntityById(long id) throws Exception;
 public List getEntityList() throws Exception;
 public boolean deleteEntity(long id) throws Exception;
}



\src\main\java\com\beingjavaguys\services\DataServicesImpl.java
package com.beingjavaguys.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.beingjavaguys.dao.DataDao;
import com.beingjavaguys.model.Employee;

public class DataServicesImpl implements DataServices {

 @Autowired
 DataDao dataDao;
 
 @Override
 public boolean addEntity(Employee employee) throws Exception {
  return dataDao.addEntity(employee);
 }

 @Override
 public Employee getEntityById(long id) throws Exception {
  return dataDao.getEntityById(id);
 }

 @Override
 public List getEntityList() throws Exception {
  return dataDao.getEntityList();
 }

 @Override
 public boolean deleteEntity(long id) throws Exception {
  return dataDao.deleteEntity(id);
 }

}

In this particular article we came across "Spring 4 Restful Web Services With Hibernate 4 Example using Maven", in our next discussion we will see how to create a Client Application in Spring to consume Spring Restful Services.
Download complete Project : Download Link
NOTE : In case you want to test the application with some dummy data and without using any client application, you can do that using Chrome's Postman - REST Client. Make sure to set content-type: application/json when sending form data in JSON format to the service.










Thanks for reading !
Being Java Guys Team

Download "Spring 4 Restful Web Services With Hibernate 4 Example" from "SkyDrive"




32 comments:

  1. Use EntityManager instead of SessionFactory. Return @ResponseStatus( HttpStatus.CREATED ) instead of custom Status.

    ReplyDelete
  2. Hi,
    Your example is very good. i have deployed it into tomcat and everything is working fine, except create(POST), iam getting 415 unsupported mediatype error in RestClient.
    My json object is as follows,
    [
    {
    "id": 4,
    "firstName": "praveen",
    "lastName": "kumar",
    "email": "prav",
    "phone": "99999999"
    }
    ]

    Can you please tell me how can i sort out this problem.
    Kind Regards.

    ReplyDelete
  3. try to send request with

    content-type : application/json, in request header

    ReplyDelete
    Replies
    1. hi, thanks for your effort. I'm trying to test this in the pivotal tc server, included with the string sts. My question is ¿What should be the full url, in order to create an employee? i.e:(in the pivotal de default path is localhost:8080/spring) the the url shoul be something like this localhost:8080/string/employee/create???????

      Thanks in advence

      Delete
  4. Thanks for your reply, i was sending content-type APPLICATION_JSON instead of application/json, now it is working like a charm.

    Thanks,
    Praveen

    ReplyDelete
  5. Hi I have tried to use your code but it is not working it is giving the below error :-

    no mapping found for http request with uri in dispatcherservlet with name 'mvc-dispatcher'

    ReplyDelete
  6. I want only Rest and hibernate Is it possible or not?

    ReplyDelete
  7. I'm getting following error. Could anyone please help me out?
    java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?
    at com.google.gson.internal.bind.TypeAdapters$1.write(TypeAdapters.java:64)
    at com.google.gson.internal.bind.TypeAdapters$1.write(TypeAdapters.java:61)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapter

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  8. Hi, I run this code in tomcat and i get the hello world screen. How to add employee ?

    ReplyDelete
    Replies
    1. I got it how to add employee. url is http://localhost:8080/SpringRestCrud/employee/create
      As advised by Nagesh change content type to application/json and use the above sample json to POST

      Delete
    2. how to add employee? pls post complete url to insert record through url

      Delete
  9. i can get the list works, how to get one employee record with id? I tried several ways,
    http://localhost:8080/SpringRestCrud/employee?id=2
    http://localhost:8080/SpringRestCrud/employee/2
    http://localhost:8080/SpringRestCrud/employee;id=2
    but none of them works.

    ReplyDelete
    Replies
    1. Hi Pls publish the URL how you got the list.....thanks

      Delete
  10. Hi I got an error with this in the bean config. please help.

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.beingjavaguys.dao.DataDaoImpl] for bean with name 'dataDao' defined in ServletContext resource [/WEB-INF/spring-config.xml]; nested exception is java.lang.ClassNotFoundException: com.beingjavaguys.dao.DataDaoImpl
    Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.beingjavaguys.dao.DataDaoImpl] for bean with name 'dataDao' defined in ServletContext resource [/WEB-INF/spring-config.xml]; nested exception is java.lang.ClassNotFoundException: com.beingjavaguys.dao.DataDaoImpl
    Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.beingjavaguys.services.DataServicesImpl] for bean with name 'dataServices' defined in ServletContext resource [/WEB-INF/spring-config.xml]; nested exception is java.lang.ClassNotFoundException: com.beingjavaguys.services.DataServicesImpl

    ReplyDelete
  11. how to add employee pls post complete url.
    i given content-type:application/json in header
    and i inserted record like this
    http://localhost:8089/SpringRhttp://localhost:8089/SpringRestCrud/employee/create?[{"id":6,"firstName":"bhavana","lastName":"hegde","email":"mml@gmail.com","phone":"90000243"}]

    ReplyDelete
  12. sir i used ur project but i have a large database scenario with many to many relationships..
    i have used all the hibernate annotations fro relationships but the entries is not updated to the many to many relationship table could any1 suggest me a solution

    ReplyDelete
  13. Pls publish the URL to get list....iam trying to access list by using this URL http://localhost:8086/Hibernate4/employee/list ..........but no response/error

    ReplyDelete
  14. Could be funny to have response about creation. Thanks in advance :)

    ReplyDelete
  15. Nice Post. Spring 4 supports Servlet 3, so we can also use DispatcherServlet as

    public class WebAppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();

    ctx.register(AppConfig.class);

    ctx.setServletContext(servletContext);

    Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));

    dynamic.addMapping("/");

    dynamic.setLoadOnStartup(1);

    }

    }

    Find the link http://www.concretepage.com/spring-4/spring-4-mvc-example-using-maven-eclipse

    ReplyDelete
  16. i can't to open this project in netbeans ?

    ReplyDelete
  17. why i can't to open project with netbeans

    ReplyDelete
  18. Hi, Thank you for this tutorial, can you please show us how to add a new employee, how the url will look like ?
    what to change in the controller ?
    Thanks, your help is appreciated.

    ReplyDelete
  19. Hendrik Tekayadi23 April 2015 at 19:55

    I've been practicing your tutorial for 2 days and now I can run smoothly :D
    Let me share what I get:
    1. look thoroughly when you CONFIGURING BUILD PATH..
    provide repository and library needed
    2. when your project run smoothly, you'll see "Hello World" by accessing http://localhost:8080/SpringRestCrud/

    3. if you run SQL script, doing the step accordingly, then access http://localhost:8080/SpringRestCrud/
    you'll get {"id":2,"firstName":"Hoston","lastName":"lindey","email":"hl@gmail.com","phone":"90908989899"}

    4.to do Post test, I use WizTools (http://java.dzone.com/announcements/wiztoolsorg-restclient-21-rele),
    set content-type:application/json
    with url: localhost:8089/SpringRhttp://localhost:8089/SpringRestCrud/employee/create?
    data: {"firstName": "rheza","lastName": "andhika","email": "yahoo","phone": "55"}
    Basically, we don't need to insert Id because it was set auto increment

    Happy learning.

    ReplyDelete
  20. ry to send request with

    content-type : application/json, in request header

    ReplyDelete
  21. With context to above given example, Is there any way to print pretty JSON?

    ReplyDelete
  22. http://localhost:8080/SpringRestCrud/employee/2 works for me.
    But i changed the url-pattern in the web.xml to
    /*

    ReplyDelete
  23. how can i add new employee ...please help me

    ReplyDelete
  24. Hi, Thank you for this tutorial. I am getting an error that "Class Not found exception DisptacherServlet". Can you please mention how to solve the issue?

    ReplyDelete
  25. I have download the code, I have maven 3.1.1 installed and eclipse. My question is: How to run the project?

    ReplyDelete
  26. I have download code. I got mvn installed and project in eclipse. How to run it?

    ReplyDelete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft