This tutorial walks you through the process of creating an application that accesses graph-based data through a hypermedia-based RESTful front end. You’ll build a Spring application that let’s you create and retrieve Employee and Address objects stored in a Neo4j NoSQL database (Neo4j Server) using Spring Data REST. You’ll use the non embedded Neo4j for achieving the exporter, and the Tomcat 8 for running the Spring Boot application.
Also read:
1. Install Eclipse Tomcat 8 Plugin
You have to install and use the Tomcat 8 and JDK 1.7 to run the example application used in this tutorial. Eclipse IDE provides you the Tomcat 8 plugin that can be downloaded from here. Tomcat 8 support is not added to the Eclipse at the time of writing this tutorial. So, download the eclipse from the given link and use it.
After downloading the plugin you have to follow the below steps for completing the plugin installation:
- Copy the downloaded file beside your root eclipse folder. Let you have an installed eclipse at D:\eclipse, the zip file should be beside the eclipse not within it.
- Extract the zip file using extract here.
- Restart the eclipse ide.
- Install Tomcat 8 from here.
- Extract your downloaded Tomcat at your preferred folder.
- Add a new server using the new server wizard and make sure you have selected the Tomcat 8.
- While you are installing the Tomcat 8 through wizard, you will be enforced to use a JDK .17 for completing the installation.
Tomcat 8 will not be shown in this tutorial, but it will be important in the next part when a Spring Boot pure Java Application converted into WAR. But the minimum requirement for running the Tomcat 8 is Java 7.
2. Java Beans
It’s the beans (Address and Employees) that will be used for REST exporting process, by which all of the persisted entities inside the Neo4j database have exposed using HTTP.
Address.java
package net.javabeat.springdata.data; import org.springframework.data.neo4j.annotation.GraphId; import org.springframework.data.neo4j.annotation.NodeEntity; @NodeEntity public class Address{ @GraphId private Long id; private String addressCountry; private String addressCity; public Long getId() { return id; } public void setId(Long 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 org.springframework.data.neo4j.annotation.Fetch; import org.springframework.data.neo4j.annotation.GraphId; import org.springframework.data.neo4j.annotation.NodeEntity; import org.springframework.data.neo4j.annotation.RelatedTo; @NodeEntity public class Employee{ @GraphId private Long id; private String employeeName; @Fetch @RelatedTo(type="Address") private Address address = new Address(); public Long getId() { return id; } public void setId(Long 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
Create the spring data repositories for the each persistence type and leave the actual implementation to the spring data run time.
EmployeeRepositories.java
package net.javabeat.springdata.repo; import net.javabeat.springdata.data.Employee; import org.springframework.data.neo4j.repository.GraphRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; @RepositoryRestResource(collectionResourceRel = "employee", path = "employee") public interface EmployeeRepository extends GraphRepository<Employee>{}
AddressRepositories.java
package net.javabeat.springdata.repo; import net.javabeat.springdata.data.Address; import org.springframework.data.neo4j.repository.GraphRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; @RepositoryRestResource(collectionResourceRel = "address", path = "address") public interface AddressRepository extends GraphRepository<Address>{}
4. Executable Application
It’s the main application that uses the Spring Boot concept to execute the Spring Application. It contains the all configuration required for setting up the context as all of those required information such as the GraphDatabaseService, base package and the repositories package. Executable main class got executed using the normal execution for any Java Class which contains a main method. Spring Boot will use the Embedded Tomcat
Executable.java
package net.javabeat.springdata.executable; import org.neo4j.graphdb.GraphDatabaseService; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.data.neo4j.config.EnableNeo4jRepositories; import org.springframework.data.neo4j.config.Neo4jConfiguration; import org.springframework.data.neo4j.rest.SpringRestGraphDatabase; import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; @Configuration @EnableNeo4jRepositories("net.javabeat.springdata.repo") @Import(RepositoryRestMvcConfiguration.class) @EnableAutoConfiguration public class Executable extends Neo4jConfiguration { public Executable() { } @Bean(destroyMethod = "shutdown") public GraphDatabaseService graphDatabaseService() { SpringRestGraphDatabase service = new SpringRestGraphDatabase("http://localhost:7474/db/data/"); this.setBasePackage("net.javabeat"); return service; } public static void main(String[] args) { SpringApplication.run(Executable.class, args); } }
5. Maven Dependencies
This pom.xml maven file defines all the dependencies used in this tutorial.
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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>next.javabeat.jsf</groupId> <artifactId>JavaBeat-Neo4j-REST</artifactId> <packaging>jar</packaging> <version>1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.0.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-neo4j</artifactId> <version>3.0.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-neo4j-rest</artifactId> <version>3.0.1.RELEASE</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-rest-webmvc</artifactId> </dependency> </dependencies> <properties> <!-- use UTF-8 for everything --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <start-class>net.javabeat.springdata.executable.Executable</start-class> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <url>http://repo.spring.io/libs-snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-milestones</id> <url>http://repo.spring.io/libs-snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>
6. Neo4j REST Demo
It’s just a demonstration for the form of the JSON response, once an HTTP request has been sent from the browser. For exposing the JSON reponse, we used the Google HTTP DEV plugin.