How to use Spring Boot, Spring Data, and Neo4j together.
Spring Data Neo4j enables convenient integration of Neo4j in your Spring-based application. It provides object-graph mapping (OGM) functionality and other features common to the Spring Data projects.
Note
|
This project uses Spring Data Neo4j 6. The previous version, using SDN 5 + OGM can still be accessed under the https://github.com/neo4j-examples/movies-java-spring-data-neo4j/tree/sdn5+ogm branch. |
The example project is described in detail on the Neo4j Developer Site
The project uses Java 11.
First, you need to have a local Neo4j running. The easiest way is Docker. If you have a docker installed locally, than please run
docker run --publish=7474:7474 --publish=7687:7687 -e 'NEO4J_AUTH=neo4j/secret' neo4j:4.1
Another option is to use Neo4j desktop, which can be downloaded from here: http://neo4j.com/download. If you chose that way, install and start as instructed by the desktop application. You need to start a new database inside desktop.
-
After you have Neo4j up and running, open up a browser and goto http://localhost:7474. Enter the credentials (
neo4j:secret
if you chose the Docker approach above or the credentials you supplied in Neo4j Desktop). Then -
Run
:play movies
command, and click and run the Cypher statement to insert the dataset -
Clone this project from GitHub
-
Update
src/main/resources/application.properties
with the username and password you set above. -
Run the project with
./mvnw spring-boot:run
To use Neo4j with Spring Data Neo4j, you just add the dependency for Spring-Boot and Spring-Data-Neo4j to your build setup.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
Describe your domain model with @Node
, @Relationship
and @RelationshipProperties
.
@Node
public class Movie {
@Id
private final String title;
@Property("tagline")
private final String description;
@Relationship(type = "ACTED_IN", direction = Direction.INCOMING)
private List<Actor> actors = new ArrayList<>();
@Relationship(type = "DIRECTED", direction = Direction.INCOMING)
private List<Person> directors = new ArrayList<>();
private Integer released;
public Movie(String title, String description) {
this.title = title;
this.description = description;
}
}
@RelationshipProperties
public class Actor {
@TargetNode
private final Person person;
private List<String> roles = new ArrayList<>();
public Actor(Person person) {
this.person = person;
}
}
@Node
public class Person {
@Id
private final String name;
private Integer born;
public Person(Integer born, String name) {
this.born = born;
this.name = name;
}
}
Those are interface based DAO implementation.
Depending whether you extend from Repository
, CrudRepository
or Neo4jRepository
, they declare a lot of functionality
out of the box.
We went with a minimalistic "declare things as needed"-approach.
interface MovieRepository extends Repository<Movie, String> {
List<Movie> findAll();
Optional<Movie> findById(String title);
Optional<Movie> findOneByTitle(String title);
List<Movie> findAllByTitleLikeIgnoreCase(String title);
List<Movie> findAllByDirectorsName(String name);
List<Movie> findAllByActorsPersonName(String name);
}
In addition, you can choose to use the Neo4jTemplate
to access your domain without repositories or the Neo4jClient
to access the Neo4j database inside Spring transactions but without mapping at all.
And last but not least, you can use the pure driver as we show in MovieService
. We have no need for Spring transactions
or mapping here, as we want to compute just a different graph representation for a front end visualization.
We added https://springdoc.org to documente our rest endpoints.
These are the components of our Web Application:
-
Application Type: Spring-Boot Java Web Application
-
Web framework: Spring-Boot enabled Spring-WebMVC, Spring-Data-Rest
-
Persistence Access: Spring-Data-Neo4j 6.x
-
Database: Neo4j-Server 4.x
-
Frontend: jquery, bootstrap, d3.js
-
OpenAPI Documentation: SpringDoc
Goto http://localhost:8080/docs to access the OpenAPI Swagger documentation for the endpoints we expose. The generated documentation comes with cURL commands you can run in most environments from your favorite terminal.
Goto http://localhost:8080 for a visualization and some interaction with this web application. The REST endpoints are designed to be used with the frontend of https://github.com/neo4j-examples/neo4j-movies-template.