我正在尝试设置https://neo4j.com/developer/example-project/
上的 Spring Data Neo4j 4 neo4j电影示例代码链接位于https://github.com/neo4j-examples/movies-java-spring-data-neo4j-4
我已经掌握了一切并在本地运行。三个组件中的两个正在工作,电影列表和背景图。什么是无效的是在右边显示一部个人电影。
此信息从以下代码开始填充 index.html (从第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>
未调用函数function (data)
。
转到http://localhost:8080/movies/search/findByTitle?title=The%20Matrix%20Reloaded
会返回以下格式错误的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"
}
}
}
未正确填充ID。我正在尝试运行此示例以开始学习spring-boot和neo4j,并且不确定为什么代码没有返回正确的数据结构?
neo4j查询有一个日志条目:
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}
我在neo4j控制台中运行它并查看输出的文本:
╒══════════════════════════════╤═══════╕
│"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"}] │ │
└──────────────────────────────┴───────┘
看起来问题出在这个额外的ID(n)列上。我不知道为什么要添加它,或者如何让它不被添加。我能看到的唯一可生成密码的代码来自 MovieRepository.java
Movie findByTitle(@Param("title") String title);
这是版本问题还是其他问题?如何让它不返回ID(n)列,或者使用正确的id:9结构返回它?
答案 0 :(得分:0)
我能够得到一个具有相同结构的不同示例: https://github.com/springframeworkguru/spring-boot-neo4j-example然后修改它以执行我原来的示例尝试执行的操作。然后,我能够比较两个项目之间的差异,将差异应用于原始项目并使其行为正确。 以下是使其正常工作的所有代码差异:
index.html (第105行)注释掉行,不需要
(105) //data = data["_embedded"].movies;
MovieService.java 添加到文件
public Movie getByTitle (String title){ return movieRepository.findByTitle (title); }
public Collection<Movie> getByTitleContaining(String title) {
return movieRepository.findByTitleContaining (title);
}
MovieController.java 添加到文件
@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 (第9行和第15行)删除或注释
(9) //import org.springframework.data.rest.core.annotation.RepositoryRestResource;
...
(15) //@RepositoryRestResource(collectionResourceRel = "movies", path = "movies")
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/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>
答案 1 :(得分:0)
截至今天,我已修复此问题:https://github.com/neo4j-examples/movies-java-spring-data-neo4j-4。
您无需更改任何代码。它会起作用。
请注意,目前这需要使用SDN 4.2.2 SNAPSHOT版本。一旦Boot和Spring Data发布他们的下一个服务版本,我们将更新项目以删除它们。