Search code examples
jsonspringneo4jjacksonspring-data-neo4j-4

neo4j movies example returning malformed json


I'm trying to setup the Spring Data Neo4j 4 neo4j movies example found at https://neo4j.com/developer/example-project/

The link to the code is at https://github.com/neo4j-examples/movies-java-spring-data-neo4j-4

I have everything up and running locally. Two of the three components are working, the list of movies and the background graph. What isn't working is the displaying of an individual movie on the right.

This information is populated starting at the following code
index.html (starting at line 82):

<script type="text/javascript">
    $(function () {
        function showMovie(title) {
            $.get("/movies/search/findByTitle?title=" + encodeURIComponent(title), // todo fix paramter in SDN
                    function (data) {
                        if (!data ) return; //  || !data["_embedded"].movies) return;
                        var movie = data; // ["_embedded"].movies[0];
                        $("#title").text(movie.title);
...
                    });
                }, "json");
        return false;
    }
...
</javascript>

The function, function (data) isn't being called.

going to http://localhost:8080/movies/search/findByTitle?title=The%20Matrix%20Reloaded is returning the following malformed JSON:

{
  "title" : "The Matrix Reloaded",
  "released" : 2003,
  "tagline" : "Free your mind",
  "roles" : [ {
    "roles" : [ "Morpheus" ],
    "person" : {
      "name" : "Laurence Fishburne",
      "born" : 1961
                    },
    "movie" : { : 9
    }
  }, {
    "roles" : [ "Agent Smith" ],
    "person" : {
      "name" : "Hugo Weaving",
      "born" : 1960
    },
    "movie" : { : 9
    }
  }, {
    "roles" : [ "Trinity" ],
    "person" : {
      "name" : "Carrie-Anne Moss",
      "born" : 1967
    },
    "movie" : { : 9
    }
  }, {
    "roles" : [ "Neo" ],
    "person" : {
      "name" : "Keanu Reeves",
      "born" : 1964
    },
    "movie" : { : 9
    }
  } ],
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/movies/9"
    },
    "movie" : {
      "href" : "http://localhost:8080/movies/9"
    }
  }
}

The id's aren't being populated correctly. I'm trying to run this example to start learning about spring-boot and neo4j and am not sure why the code isn't returning the proper data structure?

There is a log entry for the neo4j query:

MATCH (n:`Movie`) WHERE n.`title` = { `title_0` } WITH n MATCH p=(n)-[*0..1]-(m) RETURN p, ID(n) with params {title_0=The Matrix Reloaded}

I ran that in the neo4j console and looked at the outputted text:

╒══════════════════════════════╤═══════╕
│"p"                           │"ID(n)"│
╞══════════════════════════════╪═══════╡
│[]                            │"9"    │
├──────────────────────────────┼───────┤
│[{"tagline":"Free your mind","│"9"    │
│title":"The Matrix Reloaded","│       │
│released":"2003"},{"roles":["A│       │
│gent Smith"]},{"born":"1960","│       │
│name":"Hugo Weaving"}]        │       │
├──────────────────────────────┼───────┤
│[{"tagline":"Free your mind","│"9"    │
│title":"The Matrix Reloaded","│       │
│released":"2003"},{"roles":["M│       │
│orpheus"]},{"born":"1961","nam│       │
│e":"Laurence Fishburne"}]     │       │
├──────────────────────────────┼───────┤
│[{"tagline":"Free your mind","│"9"    │
│title":"The Matrix Reloaded","│       │
│released":"2003"},{"roles":["T│       │
│rinity"]},{"born":"1967","name│       │
│":"Carrie-Anne Moss"}]        │       │
├──────────────────────────────┼───────┤
│[{"tagline":"Free your mind","│"9"    │
│title":"The Matrix Reloaded","│       │
│released":"2003"},{"roles":["N│       │
│eo"]},{"born":"1964","name":"K│       │
│eanu Reeves"}]                │       │
└──────────────────────────────┴───────┘

It looks like the problem is with this additional ID(n) column. I don't know why it is being added, or how to get it not to be added. The only code I can see which would generate the cypher code is from MovieRepository.java

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

Is this a version issue, or something else? How can I get it to not return the ID(n) column, or to return it with a proper id:9 structure?


Solution

  • I was able to get a different example, with the same structure, to work: https://github.com/springframeworkguru/spring-boot-neo4j-example then modify it to do what my original example was attempting to do. Then, I was able to compare the differences between the two projects, apply the differences to the original project and have it behave correctly. Following are all the code differences to make it work correctly:

    index.html (line 105) comment out line, not needed

    (105)                    //data = data["_embedded"].movies;
    

    MovieService.java add to file

        public Movie getByTitle (String title){ return movieRepository.findByTitle (title); }
    
        public Collection<Movie> getByTitleContaining(String title) {
            return movieRepository.findByTitleContaining (title);
        }
    

    MovieController.java add to file

        @RequestMapping("/movies/search/findByTitle")
        public Movie findByTitle(@RequestParam String title){
            return movieService.getByTitle(title);
        }
    
        @RequestMapping("/movies/search/findByTitleContaining")
        public Collection<Movie> findByTitleContaining(@RequestParam String title){
            return movieService.getByTitleContaining(title);
        }
    

    MovieRepository.java (line 9 and 15) remove or comment out

    (9)  //import org.springframework.data.rest.core.annotation.RepositoryRestResource;
    ...
    (15) //@RepositoryRestResource(collectionResourceRel = "movies", path = "movies")
    

    pom.xml for readability, I put the whole file here

    <?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>com.neo4j.examples</groupId>
        <artifactId>sdn4-movies</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>SDN4 University</name>
        <description>Demo web project for Spring Boot using Spring Data Neo4j</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.1.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository --> <!-- added -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-neo4j</artifactId>
                <!--<version>4.2.0.RELEASE</version>-->
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
                <!--<artifactId>spring-boot-starter-data-neo4j</artifactId>-->
            </dependency>
    
            <dependency>
                <groupId>org.neo4j</groupId>
                <artifactId>neo4j-ogm-bolt-driver</artifactId>
                <version>2.1.1</version>
            </dependency>
    
            <!--<dependency>-->
                <!--<groupId>org.springframework.boot</groupId>-->
                <!--<artifactId>spring-boot-starter-data-rest</artifactId>-->
            <!--</dependency>-->
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <!-- For some reason newer versions of jackson cause errors with -->
            <!-- serialisation of nested objects in graph -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.8.0</version>
                <!--<version>2.5.5</version>-->
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.8.6</version>
                <!--<version>2.5.5</version>-->
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.8.6</version>
                <!--<version>2.5.5</version>-->
            </dependency>
            <!-- ========================================== -->
    
            <!-- For use in testing -->
            <dependency>
                <groupId>org.neo4j</groupId>
                <artifactId>neo4j-ogm-embedded-driver</artifactId>
                <version>2.1.1</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.neo4j</groupId>
                <artifactId>neo4j</artifactId>
                <version>3.1.1</version>
                <scope>test</scope>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>