Coder Social home page Coder Social logo

sameeranel / movies-java-spring-data-neo4j-4 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from neo4j-examples/movies-java-spring-data-neo4j

0.0 1.0 0.0 19 KB

Neo4j Movies Example with Spring Data Neo4j 4

Home Page: http://neo4j.com/developer/spring-data-neo4j

Java 68.76% HTML 31.24%

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

Spring Data Neo4j 4 with Spring Boot and Spring Data Rest - Movies Example Application

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 "movies" as password

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

  5. clone this project from GitHub

  6. 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.data</groupId>
    <artifactId>spring-data-neo4j</artifactId>
    <version>4.1.1.RELEASE</version>
</dependency>

After setting up a Java-based Spring-Boot configuration,

MyNeo4jConfiguration.java
@EnableTransactionManagement
@Import(RepositoryRestMvcConfiguration.class)
@EnableScheduling
@EnableAutoConfiguration
@ComponentScan(basePackages = {"movies.spring.data.neo4j.services"})
@Configuration
@EnableNeo4jRepositories(basePackages = "movies.spring.data.neo4j.repositories")
public class MyNeo4jConfiguration extends Neo4jConfiguration {

    public static final String URL = System.getenv("NEO4J_URL") != null ? System.getenv("NEO4J_URL") : "http://localhost:7474";

    @Bean
    public org.neo4j.ogm.config.Configuration getConfiguration() {
        org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
        config
                .driverConfiguration()
                .setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver")
                .setURI(URL);
        return config;
    }

    @Override
    public SessionFactory getSessionFactory() {
        return new SessionFactory("movies.spring.data.neo4j.domain");
    }
}

and annotating your @Node- and @RelationshipEntity, you can use the Neo4jTemplate 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 GraphRepository<Movie> {
    Movie findByTitle(@Param("title") String title);

    @Query("MATCH (m:Movie) WHERE m.title =~ ('(?i).*'+{title}+'.*') RETURN m")
    Collection<Movie> findByTitleContaining(@Param("title") String title);

    @Query("MATCH (m:Movie)<-[:ACTED_IN]-(a:Person) RETURN m.title as movie, collect(a.name) as cast LIMIT {limit}")
    List<Map<String,Object>> 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. To avoid circular JSON generation, the JSOGGenerator is used as id-generator.

See the {github}/examples/java/spring-data-neo4j[GitHub repository] for the source code.

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.1.1

  • Database: Neo4j-Server

  • 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/findByTitleContaining?title=matrix

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

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

Contributors

jexp avatar luanne avatar pdtyreus 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.