Giter Club home page Giter Club logo

movies-java-spring-data-neo4j's Introduction

Movies Example Application

How to use Spring Boot, Spring Data Neo4j and Spring Data Rest together.

Spring Data Neo4j was the first Spring Data project, started by the CEOs Rod Johnson and Emil Eifrem. It enables convenient integration of Neo4j in your Spring-based application. It provides object-graph mapping functionality and other features common to the Spring Data projects.

Note
This project uses Spring Data Neo4j 4 which is a complete rewrite from earlier versions. It is optimized for working with Neo4j Server and based on Neo4j’s query language, Cypher.

The example project is described in detail on the Neo4j Developer Site

Quickstart

  1. Download, install and start Neo4j Server.

  2. open the web-interface at http://localhost:7474

  3. configure a username and password if you haven’t already.

  4. run :play movies command, and click and run the Cypher statement to insert the dataset

  5. clone this project from GitHub

  6. update src/main/resources/application.properties with the username and password you set above.

  7. run the project with mvn spring-boot:run.

Code Walkthrough

To use Neo4j with Spring Data Neo4j, you just add the dependency for Spring-Boot and Spring-Data-Neo4j to your build setup.

pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>

Annotate your @NodeEntity and @RelationshipEntity, you can use the OGM Session to access Neo4j APIs and object graph mapping functionality.

Movie.java
@NodeEntity
public class Movie {

    @GraphId
    Long id;

    String title;

    int released;

    String tagline;

    @Relationship(type="ACTED_IN", direction = Relationship.INCOMING)
    List<Role> roles;
...
}

Additionally you can leverage the convenient Spring-Data repositories to get interface-based DAO implementations injected into your Spring Components.

MovieRepository.java
@RepositoryRestResource(collectionResourceRel = "movies", path = "movies")
public interface MovieRepository extends PagingAndSortingRepository<Movie, Long> {

	Movie findByTitle(@Param("title") String title);

	Collection<Movie> findByTitleLike(@Param("title") String title);

	@Query("MATCH (m:Movie)<-[r:ACTED_IN]-(a:Person) RETURN m,r,a LIMIT {limit}")
	Collection<Movie> graph(@Param("limit") int limit);
}

In our case we use the repository from a MovieService to compute the graph representation for the visualization. The service is then injected into our main Boot application, which also doubles as @RestMvcController which exposes the /graph endpoint.

The other two endpoints for finding multiple movies by title and loading a single movie are provided out of the box by the Spring-Data-Rest project which exposes our MovieRepository as REST endpoints.

The rendering of the movie objects (and related entities) happens automatically out of the box via Jackson mapping.

The Stack

These are the components of our Web Application:

  • Application Type: Spring-Boot Java Web Application (Jetty)

  • Web framework: Spring-Boot enabled Spring-WebMVC, Spring-Data-Rest

  • Persistence Access: Spring-Data-Neo4j 4.2.x

  • Database: Neo4j-Server 3.x

  • Frontend: jquery, bootstrap, d3.js

Endpoints:

Get Movie

// JSON object for single movie with cast
curl http://localhost:8080/movies/search/findByTitle?title=The%20Matrix

// list of JSON objects for movie search results
curl http://localhost:8080/movies/search/findByTitleLike?title=*matrix*

// JSON object for whole graph viz (nodes, links - arrays)
curl http://localhost:8080/graph

movies-java-spring-data-neo4j's People

Contributors

frant-hartm avatar jasperblues avatar jexp avatar luanne avatar mangrish avatar pdtyreus avatar szantopeter avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.