In this Spring REST tutorial, we will learn how to perform CRUD Operations (Create, Read, Update, Delete) using Spring RESTful Web Services with the support of Hibernate JPA. In REST, manipulation of the resources is performed based on the common set of HTTP verbs.
- POST – To Create a resource
- GET – To Retrieve a resource
- PUT – To Update a resource
- DELETE – To Delete a resource
Based on the above HTTP verbs, our REST API does the below
- Create Student (POST) : Create a new Student (/create)
- Get Student By Id (GET) : Get a Student based on id (/student/1)
- List of All Students (GET) : Get all the students (/students)
- Update Student (PUT) : Update a Student (/update)
- Delete Student (DELETE) : Delete a Student (/student/1)
Creating table
Create STUDENT Table, simply Copy and Paste the following SQL query in the query editor to get the table created.
CREATE TABLE "STUDENT" ( "ID" NUMBER(10,0) NOT NULL ENABLE, "NAME" VARCHAR2(255 CHAR), "AGE" NUMBER(10,0) NOT NULL ENABLE, PRIMARY KEY ("ID") ); insert into "STUDENT" values (1,'JIP1',11); insert into "STUDENT" values (2,'JIP2',22); insert into "STUDENT" values (3,'JIP3',33); insert into "STUDENT" values (4,'JIP4',44);
Folder Structure:
- Create a Maven Webapp project (maven-archetype-webapp) “SpringRestHibernate” and create a package for our source files “com.javainterviewpoint” under src/main/java
- Now add the following dependency in the 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.javainterviewpoint</groupId> <artifactId>SpringRestHibernate</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringRestHibernate Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.3.6.RELEASE</spring.version> <jstl.version>1.2</jstl.version> <hibernate.version>5.0.5.Final</hibernate.version> <jackson.version>2.8.3</jackson.version> <oracle.connector.version>11.2.0</oracle.connector.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version> </dependency> <!-- Spring mvc 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> <!-- Jackson libraries --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <!-- jstl for jsp page --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <!-- Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <!-- Oracle --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>${oracle.connector.version}</version> </dependency> <dependency> <groupId>javax.transaction</groupId> <artifactId>jta</artifactId> <version>1.1</version> </dependency> </dependencies> <build> <finalName>SpringRestHibernate</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> <version>3.0</version> </plugin> </plugins> </build> </project>
Spring RESTful Web Services CRUD Example
StudentController.java
package com.javainterviewpoint.controller; 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.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.RestController; import com.javainterviewpoint.dao.StudentDAOImpl; import com.javainterviewpoint.model.Student; @RestController public class StudentController { @Autowired private StudentDAOImpl studentDAOImpl; /*** Creating a new Student ***/ @RequestMapping(value="/create", method=RequestMethod.POST, produces="application/json", consumes="application/json") public void createStudent(@RequestBody Student student) { studentDAOImpl.createStudent(student); } /*** Retrieve a single Student ***/ @RequestMapping(value="/student/{id}",produces="application/json", method=RequestMethod.GET) public Student getStudentById(@PathVariable("id") long id) { Student student = studentDAOImpl.getStudentById(id); return student; } /*** Retrieve all Students ***/ @RequestMapping(value="/students",produces="application/json", method=RequestMethod.GET) public List getAllStudents() { List studentList = studentDAOImpl.getAllStudents(); return studentList; } /*** Update a Student ***/ @RequestMapping(value="/update", method=RequestMethod.PUT, produces="application/json", consumes="application/json") public void updateStudent(@RequestBody Student student) { studentDAOImpl.updateStudent(student); } /*** Delete a Student ***/ @RequestMapping(value="/delete/{id}",method = RequestMethod.DELETE, produces="application/json") public void deleteStudent(@PathVariable("id") long id) { studentDAOImpl.deleteStudent(id); } }
- We have annotated our “StudentController” class with @RestController, In Spring 4 @RestController annotation is introduced it is a combination of @Controller + @ResponseBody. So when using @RestController, you do not need to use @ResponseBody it is optional now
- @RequestBody: This annotation tells Spring to bind the incoming HTTP request body( object passed in the request). HTTP Message converters convert the HTTP request body into domain object based on Accept header present in the request.
- @PathVariable: This annotation will bind method argument to the URI template variable.
- We have added the “jackson-databind” as our dependency, Spring invokes inbuilt MappingJackson2HttpMessageConverter to convert the response into JSON.
StudentDAO.java
package com.javainterviewpoint.dao; import java.util.List; import com.javainterviewpoint.model.Student; public interface StudentDAO { public void createStudent(Student student); public Student getStudentById(long id); public List<Student> getAllStudents(); public void updateStudent(Student student); public void deleteStudent(long id); }
StudentDAOImpl.java
package com.javainterviewpoint.dao; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.javainterviewpoint.model.Student; @Service @Transactional(propagation = Propagation.REQUIRED) public class StudentDAOImpl implements StudentDAO { @PersistenceContext private EntityManager entityManager; @Override public void createStudent(Student student) { entityManager.persist(student); } @Override public Student getStudentById(long id) { return entityManager.find(Student.class,id); } @Override public List<Student> getAllStudents() { return entityManager.createQuery("select stu from Student stu").getResultList(); } @Override public void updateStudent(Student student) { entityManager.merge(student); } @Override public void deleteStudent(long id) { Student s = entityManager.find(Student.class,id); entityManager.remove(s); } }
StudentDAOImpl implements StudentDAO interface and overrides all its method. The @PersistanceContext annotation will let you inject EntityManager in our StudentDAOImpl class.
Student.java
package com.javainterviewpoint.model; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @Entity @Table(name="STUDENT") @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name = "Student") public class Student implements Serializable { private static final long serialVersionUID = -1232395859408322328L; @Id @Column(name="ID") private long id; @Column(name="NAME") private String name; @Column(name="AGE") private long age; public Student() { super(); } public Student(long id, String name, long age) { super(); this.id = id; this.name = name; this.age = age; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public long getAge() { return age; } public void setAge(long age) { this.age = age; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
We have used the below JPA Annotations in our Student class
- @Entity – This annotation will mark our Student class as an Entity Bean.
- @Table – @Table annotation will map our class to the corresponding database table. You can also specify other attributes such as indexes, catalog, schema, uniqueConstraints. The @Table annotation is an optional annotation if this annotation is not provided then the class name will be used as the table name.
- @JsonIgnoreProperties – This annotation suppresses serialization (JSON) of properties during reading.
- @Id – The @Id annotation marks the particular field as the primary key of the Entity.
- @GeneratedValue – This annotation is used to specify how the primary key should be generated. Here SEQUENCE Strategy will be used as this the default strategy for Oracle
- @Column – This annotation maps the corresponding fields to their respective columns in the database table.
web.xml
The web.xml has everything about the application that a server needs to know, which is placed under the WEB-INF directory. <servlet-name> contains the name of the SpringConfiguration , when the DispatcherServlet is initialized the framework will try to load a configuration file “[servlet-name]-servlet.xml” under the WEB-INF directory.
<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>Archetype Created Web Application</display-name> <servlet> <servlet-name>SpringRestHib</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SpringRestHib</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
SpringRestHib-servlet.xml
The SpringRest-servlet.xml is also placed under the WEB-INF directory.
<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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="com.javainterviewpoint" /> <mvc:annotation-driven /> <!-- Database Configurations --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@rsh2:40051:mydb" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <!-- Hiberante Sesssion Factory Configurations --> <!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="annotatedClasses"> <list> <value>com.javainterviewpoint.model.Student</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> --> <!-- Hiberante Entity Manager Configurations --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="com.javainterviewpoint.model,com.javainterviewpoint.controller,com.javainterviewpoint.dao" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> </property> <property name="jpaProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" /> </beans>
- <context:component-scan> will let the Spring Container to search for all the annotation under the package “com.javainteriviewpoint”.
- <mvc:annotation-driven/> annotation will activate the @Controller, @RequestMapping, @Valid etc annotations.
- We have defined the below beans in our SpringConfig file.
- dataSource : This bean holds all the database related configurations such as driverClassName, url, username, password.
- entityManagerFactoryBean: This is the important bean where in which we will be passing the datasource reference and set values to the properties jpaVendorAdapter, jpaProperties
- transactionManager: We are using the JpaTransactionManager for managing the transactions for our application, we will be passing the entityManagerFactoryBean reference to it.
Output :
Create a user
In POSTMAN, select POST method, select the type as “application/json” and give the url as “http://localhost:8080/SpringRestHibernate/create”. Add the JSON object which we are going to pass in the Body
{
“id”:5,
“name”:”JIP5″,
“age”:55
}
POSTMAN will automatically add a header Content-Type as “application/json”, Now click on Send
You will get the Status as 200, which confirms that the Student has been created.
Retrieve a single student
In POSTMAN, select GET method, and give the url as “http://localhost:8080/SpringRestHibernate/student/3”.
Retrieve all students
select GET method, and give the url as “http://localhost:8080/SpringRestHibernate/students”
Update Student
select PUT method, select the type as “application/json” and give the url as “http://localhost:8080/SpringRestHibernate/update”. Add the JSON object which we are going to pass in the Body
{
“id”:3,
“name”:”JIP3333″,
“age”:33
}
Delete Student
select DELETE method, and give the url as “http://localhost:8080/SpringRestHibernate/delete/3”
Spring RestTemplate
Spring RestTemplate communicates with the HTTP server using REST principals. It uses HTTP verbs such as GET, POST, HEAD, PUT, DELETE etc. RestTemplate provides different methods to communicate that will accept URI template, URI variables, response type and request object as arguments. Below are some of the common methods of RestTemplate
DELETE | delete(java.lang.String, java.lang.Object…) |
GET | getForObject(java.lang.String, java.lang.Class, java.lang.Object…) |
getForEntity(java.lang.String, java.lang.Class, java.lang.Object…) | |
HEAD | headForHeaders(java.lang.String, java.lang.Object…) |
OPTIONS | optionsForAllow(java.lang.String, java.lang.Object…) |
POST | postForLocation(java.lang.String, java.lang.Object, java.lang.Object…) |
postForObject(java.lang.String, java.lang.Object, java.lang.Class, java.lang.Object…) | |
PUT | put(java.lang.String, java.lang.Object, java.lang.Object…) |
package com.javainterviewpoint.controller; import java.util.List; import java.util.Map; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.web.client.RestTemplate; import com.javainterviewpoint.model.Student; public class SpringRestTemplateClient { public static final String REST_BASE_URI = "http://localhost:8080/SpringRestHibernate"; static RestTemplate restTemplate = new RestTemplate(); /**POST**/ public static void createStudent() { Student student = new Student(); student.setId(5); student.setName("JIP"); student.setAge(5); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); //headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); HttpEntity entity = new HttpEntity<>(student,headers); restTemplate.postForObject(REST_BASE_URI+"/create", entity,Student.class); } /**GET**/ private static void getStudent(int id) { Student student = restTemplate.getForObject(REST_BASE_URI+"/student/"+id, Student.class); System.out.println("**** Student with id : "+id+"****"); System.out.println("Id :"+student.getId()+" Name : "+student.getName()+" Age : "+student.getAge()); } public static void getAllStudents() { List<Map<String, Object>> studentList = restTemplate.getForObject(REST_BASE_URI + "/students", List.class); if (studentList != null) { System.out.println("**** All Students ****"); for (Map<String, Object> map : studentList) { System.out.println("Id : id=" + map.get("id") + " Name=" + map.get("name") + " Age=" + map.get("age")); } } else { System.out.println("No Students exist!!"); } } /**PUT**/ public static void updateStudent() { Student student = new Student(); student.setId(5); student.setName("JIP55"); student.setAge(55); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity entity = new HttpEntity<>(student,headers); restTemplate.put(REST_BASE_URI + "/update", entity,Student.class); } /**DELETE**/ public static void deleteStudent(int id) { restTemplate.delete(REST_BASE_URI + "/delete/"+id); } public static void main(String args[]) { createStudent(); getAllStudents(); getStudent(2); updateStudent(); deleteStudent(5); } }
Output:
**** All Students **** Id : id=1 Name=JIP1 Age=11 Id : id=2 Name=JIP2 Age=22 Id : id=5 Name=JIP Age=5 **** Student with id : 2**** Id :2 Name : JIP2 Age : 22
ravi says
Thanku somuch
Kumaravel says
Nice tutorial this