Spring Data REST is built on top of the Spring Data repositories and it automatically exposes the repositories as REST services. Spring Data REST takes the features of Spring HATEOAS and Spring Data JPA and combines them together, using a Strategy called “RepositoryDetectionStrategy” to export the repository as a REST resource. In this Spring Data REST Example, let’s create a Spring Boot RESTful API and perform CRUD Operations
It uses the HATEOAS (Hypermedia As The Engine Of Application State) principle and supports HAL (Hypertext Application Language). HATEOAS principle is that each resource should have its own URI / endpoint and it is the basic rule which all the RESTful Web Services must follow.
Spring Data REST automagically creates a Spring REST Web Service without the need for us to explicitly write one. We will have to use spring-boot-starter-data-rest dependency along with spring-boot-starter-data-jpa in order to create the REST API
Let’s get started
Creating table
Create EMPLOYEE Table, simply Copy and Paste the following SQL query in the MySQL query editor to get the table created
CREATE TABLE EMPLOYEE ( ID INT NOT NULL AUTO_INCREMENT, FIRSTNAME VARCHAR(30), LASTNAME VARCHAR(30), PRIMARY KEY(ID) ); insert into Employee (ID,FIRSTNAME,LASTNAME) values(101, 'John','Smith'); insert into Employee (ID,FIRSTNAME,LASTNAME) values(102, 'David','Jones');
Spring Data REST Example – Spring Boot RESTful API
Folder Structure:
- Create a simple Maven Project “SpringDataRest” by selecting maven-archetype-quickstart 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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javainterviewpoint</groupId> <artifactId>SpringDataRest</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringDataRest</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
- Create the Java classes App.java, Employee.java and EmployeeRepository under com.javainterviewpoint folder.
- Create application.properties file under src/main/resources directory and provide the MySQL Connection settings and JPA Properties
#MySQL Connection settings spring.datasource.driver = com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=root #JPA properties spring.jpa.show-sql = true spring.jpa.hibernate.ddl-auto = update spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
The spring-boot-starter-parent is a special starter, it provides useful Maven defaults.
spring-boot-starter-data-jpa uses Spring Data JPA with Hibernate as the implementation, which enables the easy implementation of JPA based repositories.
spring-boot-starter-data-rest has a specific purpose it help us to expose repositories as a REST services.
Dependency Tree
[INFO] ---------------< com.javainterviewpoint:SpringDataRest >---------------- [INFO] Building SpringDataRest 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-dependency-plugin:3.1.1:tree (default-cli) @ SpringDataRest --- [INFO] com.javainterviewpoint:SpringDataRest:jar:0.0.1-SNAPSHOT [INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:2.1.3.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-aop:jar:2.1.3.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot-starter:jar:2.1.3.RELEASE:compile [INFO] | | | +- org.springframework.boot:spring-boot:jar:2.1.3.RELEASE:compile [INFO] | | | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.1.3.RELEASE:compile [INFO] | | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.1.3.RELEASE:compile [INFO] | | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile [INFO] | | | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile [INFO] | | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.11.2:compile [INFO] | | | | | \- org.apache.logging.log4j:log4j-api:jar:2.11.2:compile [INFO] | | | | \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile [INFO] | | | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile [INFO] | | | \- org.yaml:snakeyaml:jar:1.23:runtime [INFO] | | +- org.springframework:spring-aop:jar:5.1.5.RELEASE:compile [INFO] | | \- org.aspectj:aspectjweaver:jar:1.9.2:compile [INFO] | +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.1.3.RELEASE:compile [INFO] | | +- com.zaxxer:HikariCP:jar:3.2.0:compile [INFO] | | \- org.springframework:spring-jdbc:jar:5.1.5.RELEASE:compile [INFO] | +- javax.transaction:javax.transaction-api:jar:1.3:compile [INFO] | +- javax.xml.bind:jaxb-api:jar:2.3.1:compile [INFO] | | \- javax.activation:javax.activation-api:jar:1.2.0:compile [INFO] | +- org.hibernate:hibernate-core:jar:5.3.7.Final:compile [INFO] | | +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile [INFO] | | +- javax.persistence:javax.persistence-api:jar:2.2:compile [INFO] | | +- org.javassist:javassist:jar:3.23.1-GA:compile [INFO] | | +- net.bytebuddy:byte-buddy:jar:1.9.10:compile [INFO] | | +- antlr:antlr:jar:2.7.7:compile [INFO] | | +- org.jboss:jandex:jar:2.0.5.Final:compile [INFO] | | +- com.fasterxml:classmate:jar:1.4.0:compile [INFO] | | +- org.dom4j:dom4j:jar:2.1.1:compile [INFO] | | \- org.hibernate.common:hibernate-commons-annotations:jar:5.0.4.Final:compile [INFO] | +- org.springframework.data:spring-data-jpa:jar:2.1.5.RELEASE:compile [INFO] | | +- org.springframework.data:spring-data-commons:jar:2.1.5.RELEASE:compile [INFO] | | +- org.springframework:spring-orm:jar:5.1.5.RELEASE:compile [INFO] | | +- org.springframework:spring-context:jar:5.1.5.RELEASE:compile [INFO] | | | \- org.springframework:spring-expression:jar:5.1.5.RELEASE:compile [INFO] | | +- org.springframework:spring-tx:jar:5.1.5.RELEASE:compile [INFO] | | +- org.springframework:spring-beans:jar:5.1.5.RELEASE:compile [INFO] | | +- org.springframework:spring-core:jar:5.1.5.RELEASE:compile [INFO] | | | \- org.springframework:spring-jcl:jar:5.1.5.RELEASE:compile [INFO] | | \- org.slf4j:slf4j-api:jar:1.7.25:compile [INFO] | \- org.springframework:spring-aspects:jar:5.1.5.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter-data-rest:jar:2.1.3.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-web:jar:2.1.3.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot-starter-json:jar:2.1.3.RELEASE:compile [INFO] | | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.8:compile [INFO] | | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.8:compile [INFO] | | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.8:compile [INFO] | | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.1.3.RELEASE:compile [INFO] | | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.16:compile [INFO] | | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.16:compile [INFO] | | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.16:compile [INFO] | | +- org.hibernate.validator:hibernate-validator:jar:6.0.14.Final:compile [INFO] | | | \- javax.validation:validation-api:jar:2.0.1.Final:compile [INFO] | | +- org.springframework:spring-web:jar:5.1.5.RELEASE:compile [INFO] | | \- org.springframework:spring-webmvc:jar:5.1.5.RELEASE:compile [INFO] | \- org.springframework.data:spring-data-rest-webmvc:jar:3.1.5.RELEASE:compile [INFO] | \- org.springframework.data:spring-data-rest-core:jar:3.1.5.RELEASE:compile [INFO] | +- org.springframework.hateoas:spring-hateoas:jar:0.25.1.RELEASE:compile [INFO] | +- org.springframework.plugin:spring-plugin-core:jar:1.2.0.RELEASE:compile [INFO] | \- org.atteo:evo-inflector:jar:1.2.2:compile [INFO] +- mysql:mysql-connector-java:jar:8.0.15:compile [INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.8:compile [INFO] | \- com.fasterxml.jackson.core:jackson-core:jar:2.9.8:compile [INFO] \- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile [INFO] ------------------------------------------------------------------------
Employee.java
package com.javainterviewpoint; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import org.springframework.data.rest.core.annotation.RestResource; @Entity @RestResource @Table(name="EMPLOYEE") public class Employee { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="ID") private Integer id; @Column(name="FIRSTNAME") private String firstname; @Column(name="LASTNAME") private String lastname; public Employee() { super(); } public Employee(Integer id, String firstname, String lastname) { super(); this.id = id; this.firstname = firstname; this.lastname = lastname; } public Integer getId() { return id; } public void setId(Integer 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; } }
Our Employee class is our JPA entity class consisting of the getters and setters for the Employee class properties (id, firstname, lastname).
In the POJO class, we have used the below JPA Annotations.
- @Entity – This annotation will mark our Employee 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.
- @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 MySQL
EmployeeRepository.java
package com.javainterviewpoint; import org.springframework.data.repository.CrudRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; @RepositoryRestResource(collectionResourceRel = "employee", path = "employee") public interface EmployeeRepository extends CrudRepository<Employee, Integer> { }
Spring Data JPA will create an implementation of EmployeeRepository automatically during runtime, all we need to do is just extend the CrudRepository. We have annotated the EmployeeRespository class with @RepositoryRestResource annotation, this annotation will map all the end points based on our repository.
App.java
package com.javainterviewpoint; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main( String[] args ) { SpringApplication.run(App.class); } }
@SpringBootApplication annotation does the work of @EnableAutoConfiguration, @Configuration, and @ComponentScan annotations together
That’s it we have created our RESTful Web Services, Seriously, once we have created our Entity (Employee) and Repository (EmployeeRepository) we can throw request to our REST API
Let’s Run our Application!
Select the Project –>Run As –> Run Configuration –>Maven –> New Configuration. In the Main tab, key in the Goals as “spring-boot:run” and click on Run.
Use POSTMAN or any other Rest Client and hit on the url http://localhost:8080/
You will see the HATEOAS representation of all the available REST URLs
{ "_links": { "employee": { "href": "http://localhost:8080/employee" }, "profile": { "href": "http://localhost:8080/profile" } } }
- _links is part of the HAL, it exposes the links of the endpoints which is available.
- employee is the Service which is created by Spring Data REST from our repository.
- profile exposes the additional metadata information
Now hit on the URL http://localhost:8080/employee, it displays the list of the employees available.
{ "_embedded": { "employee": [ { "firstname": "John", "lastname": "Smith", "_links": { "self": { "href": "http://localhost:8080/employee/101" }, "employee": { "href": "http://localhost:8080/employee/101" } } }, { "firstname": "David", "lastname": "Jones", "_links": { "self": { "href": "http://localhost:8080/employee/102" }, "employee": { "href": "http://localhost:8080/employee/102" } } } ] }, "_links": { "self": { "href": "http://localhost:8080/employee" }, "profile": { "href": "http://localhost:8080/profile/employee" } } }
POST Request (Create)
We have already created 2 employees directly using SQL, let’s try creating the third employee using Spring Data Rest POST
Using POSTMAN, Place a POST Request by hitting the URL http://localhost:8080/employee with the below body and Content-Type:application/json
{ "firstname": "Paul", "lastname": "Scholes" }
Once we executed the above request, we will be getting 201 response code and the new employee which is created as a JSON
{ "firstname": "Paul", "lastname": "Scholes", "_links": { "self": { "href": "http://localhost:8080/employee/1" }, "employee": { "href": "http://localhost:8080/employee/1" } } }
GET Request (Read)
Using POSTMAN, Place a GET Request by hitting the URL http://localhost:8080/employee
We will be getting the below response, _embedded displays all the employees which is available in the database.
{ "_embedded": { "employee": [ { "firstname": "Paul", "lastname": "Scholes", "_links": { "self": { "href": "http://localhost:8080/employee/1" }, "employee": { "href": "http://localhost:8080/employee/1" } } }, { "firstname": "John", "lastname": "Smith", "_links": { "self": { "href": "http://localhost:8080/employee/101" }, "employee": { "href": "http://localhost:8080/employee/101" } } }, { "firstname": "David", "lastname": "Jones", "_links": { "self": { "href": "http://localhost:8080/employee/102" }, "employee": { "href": "http://localhost:8080/employee/102" } } } ] }, "_links": { "self": { "href": "http://localhost:8080/employee" }, "profile": { "href": "http://localhost:8080/profile/employee" } } }
PUT Request (Update)
Now let’s try to update the employee with id 1
Using POSTMAN, Place a PUT Request, hit the URL http://localhost:8080/employee/1 with the below body and Content-Type:application/json
we have added -edited to the lastname
{ "firstname": "Paul", "lastname": "Scholes-edited" }
we will be getting 200 response code and the updated employee as JSON
{ "firstname": "Paul", "lastname": "Scholes-edited", "_links": { "self": { "href": "http://localhost:8080/employee/1" }, "employee": { "href": "http://localhost:8080/employee/1" } } }
DELETE Request (Delete)
Let’s try deleting the employee with id 101
Using POSTMAN, Place a DELETE Request by hitting the URL http://localhost:8080/employee/101
We will be getting will get 204 No content response code, as the particular employee will not be available after delete
Searching
Till now we were able to search only with the employee id but Spring Data REST has the capabilities which allow you to search with different criteria as well.
Lets search with the below criteria
- With FirstName
- With Id and LastName
We need to a minor modification to our EmployeeRepository by adding the below methods.
package com.javainterviewpoint; import java.util.List; import org.springframework.data.repository.CrudRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; @RepositoryRestResource(collectionResourceRel = "employee", path = "employee") public interface EmployeeRepository extends CrudRepository<Employee, Integer> { List<Employee> findByFirstname(String firstname); List<Employee> findByIdAndLastname(Integer id, String lastname); }
Spring REST will expose endpoints for the methods findByFirstName() and findByIdAndLastname()
1. With FirstName
Hit on the URL http://localhost:8080/employee/search/findByFirstname?firstname=David
Spring Data will automatically generate the query behind the scene “SELECT * FROM EMPLOYEE WHERE FIRSTNAME = ?”
2. With Id and LastName
Hit on the URL http://localhost:8080/employee/search/findByIdAndLastname?id=1&lastname=Scholes-edited
The query generated “SELECT * FROM EMPLOYEE WHERE ID= ? AND LASTNAME = ?”
Hope this article helps you in getting the basic understanding of Spring Data REST. Happy Learning !!
Leave a Reply