This tutorial guides you through an example for understanding the integration between Spring Data, Spring REST and MongoDB. As you are going to use the rest-shell for achieving different operations against database. Entities will be persisted into MongoDB in the form of which an outer entity will save a reference to the inner one. Unlike the most of the examples that are published in previous tutorials here. Also, you’d see the integrating of Spring Boot for initializing of an embedded Apache Tomcat.
Also read:
1. Tools Required
The required platforms and environments for this tutorial:
- JDK 1.7.
- Maven 3.2.1.
- Eclipse IDE Kepler 4.3.
- Spring Data
- MongoDB
2. Java Beans (Entities)
Address and Employees entities will be used for defining the business domain.
Address.java
package net.javabeat.springdata.data; import javax.persistence.Id; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; @Document(collection="Address") public class Address { @Id @Field private String id; @Field private String addressCountry = ""; @Field private String addressCity = ""; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getAddressCountry() { return addressCountry; } public void setAddressCountry(String addressCountry) { this.addressCountry = addressCountry; } public String getAddressCity() { return addressCity; } public void setAddressCity(String addressCity) { this.addressCity = addressCity; } }
Employee.java
package net.javabeat.springdata.data; import javax.persistence.Id; import org.springframework.data.mongodb.core.mapping.DBRef; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; @Document(collection="Employee") public class Employee { @Id @Field private String id; @Field private String employeeName; @DBRef private Address address; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } }
3. Spring Data Repositories
It’s the spring data repositories that will be used for communicating with the MongoDB for any CRUD operation.
AddressRepository.java
package net.javabeat.springdata.repo; import net.javabeat.springdata.data.Address; import org.springframework.data.repository.CrudRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; @RepositoryRestResource(collectionResourceRel="address",path="address") public interface AddressRepository extends CrudRepository<Address,String>{ }
EmployeeRepository.java
package net.javabeat.springdata.repo; import net.javabeat.springdata.data.Employee; 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, String>{ }
4. Spring Context Configuration
Here is the required spring context configuration for communicating to the MongoDB.
SpringContext.xml
<?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:mongo="http://www.springframework.org/schema/data/mongo" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <!-- Register Mongo Instance --> <mongo:mongo id="mongo" host="localhost" port="27017" /> <!-- for defining mongo template --> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongo" /> <constructor-arg name="databaseName" value="JavaBeat" /> </bean> <!-- For consider the using of annotations foe defining Spring Bean --> <context:annotation-config /> <!-- For defining Spring Bean --> <context:component-scan base-package="net.javabeat.springdata.beans" /> <!-- For defining mongo repository --> <mongo:repositories base-package="net.javabeat.springdata.repo" /> </beans>
5. Executable Application
This stand alone spring boot application will start this sample application.
Executable.java
package net.javabeat.springdata.executable; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportResource; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; @Configuration @EnableMongoRepositories @Import(RepositoryRestMvcConfiguration.class) @ImportResource("classpath:SpringContext.xml") @EnableAutoConfiguration public class Executable { public static void main(String[] args) { SpringApplication.run(Executable.class, args); } }
6. Maven Dependencies
It’s the maven libraries that define the required libraries for the suggested communication with the MongoDB through Spring REST.
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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>net.javabeat.springdata</groupId> <artifactId>SpringData-MongoDB-REST</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>Spring Data</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.0.2.RELEASE</version> </parent> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <dependencies> <!-- Dependency for Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-rest-core</artifactId> <version>2.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-rest-webmvc</artifactId> </dependency> </dependencies> </project>
7. Install REST Shell
Rest shell is the native engine that provided by Spring and can be downloaded from here. After downloading the shell, you can unzip it into any location that you suggested.
Open the command line and navigate into the unzipped location that you already selected before and type into command line rest-shell and press enter. Be sure that your application is up and running before any further uses of the shell.
8. MongoDB + Spring Data Repository + Spring REST Demo
Inside this demonstration, set of snapshots has been used for clarifying the operations of READ, PERSIST & DELETE operations.
9. MongoDB Collections
10. Accessing Through HTTP
11. Summary
Congratulations, you’ve just developed an application that uses the Spring REST for getting successful operations against MongoDB.