我是neo4j和spring boot / spring mvc的新手。我尝试从neo4j.com了解电影图形数据库,并且我以相同的方式构建新数据库和应用程序,但是当我运行" localhost:8080"它只加载我创建的html文件的结构。我无法在http页面上从数据库中提取数据。
这是主要课程:
package companie.neo4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
//import companie.spring.data.neo4j.SampleCompanyApplication;
@SpringBootApplication
@EntityScan("companie.spring.data.neo4j.domain")
public class SampleCompanyApplication {
public static void main(String[] args) {
SpringApplication.run(SampleCompanyApplication.class, args);
}
}
控制器:
package companie.spring.data.neo4j.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import companie.spring.data.neo4j.services.ProjectService;
@RestController("/")
public class ProjectController {
final ProjectService projectService;
@Autowired
public ProjectController(ProjectService projectService) {
this.projectService = projectService;
}
@RequestMapping("/graph")
public Map<String, Object> graph(@RequestParam(value = "limit",required = false) Integer limit) {
return projectService.graph(limit == null ? 100 : limit);
}
}
ProgrammerRepository:
package companie.spring.data.neo4j.repositories;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
import companie.spring.data.neo4j.domain.Programmer;
public interface ProgrammerRepository extends PagingAndSortingRepository<Programmer,Long> {
}
ProjectRepository:
package companie.spring.data.neo4j.repositories;
import java.util.Collection;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import companie.spring.data.neo4j.domain.Project;
@RepositoryRestResource(collectionResourceRel = "project", path = "project")
public interface ProjectRepository extends PagingAndSortingRepository<Project, Long> {
Project findByTitle(@Param("title") String title);
Collection<Project> findByTitleLike(@Param("title") String title);
@Query("MATCH (proj:Project)<-[r:IMPLEMENTED]-(progr:Person) RETURN proj,r,progr LIMIT {limit}")
Collection<Project> graph(@Param("limit") int limit);
}
ProjectService:
package companie.spring.data.neo4j.services;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import companie.spring.data.neo4j.domain.Programmer;
import companie.spring.data.neo4j.domain.Project;
import companie.spring.data.neo4j.repositories.ProjectRepository;
import companie.spring.data.neo4j.domain.Project;
import companie.spring.data.neo4j.domain.Programmer;
import companie.spring.data.neo4j.repositories.ProjectRepository;
@Service
public class ProjectService {
@Autowired ProjectRepository projectRepository;
private Map<String, Object> toD3Format(Collection<Project> projects) {
List<Map<String, Object>> nodes = new ArrayList();
List<Map<String, Object>> rels = new ArrayList();
int i = 0;
Iterator<Project> result = projects.iterator();
while (result.hasNext()) {
Project project = result.next();
nodes.add(map("title",project.getTitle(), "label", "project"));
int target = i;
i++;
for (Programmer p : project.getProgrammer() ) {
Map<String, Object> coder = map("title", p.getName(), "label", "coder");
int source = nodes.indexOf(coder);
if (source == -1) {
nodes.add(coder);
source = i++;
}
rels.add(map("source", source, "target", target));
}
}
return map("nodes", nodes, "links", rels);
}
private Map<String, Object> map(String key1, Object value1, String key2, Object value2) {
Map<String, Object> result = new HashMap<String, Object>(2);
result.put(key1, value1);
result.put(key2, value2);
return result;
}
@Transactional(readOnly = true)
public Map<String, Object> graph(int limit) {
Collection<Project> result = projectRepository.graph(limit);
return toD3Format(result);
}
}
的index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Company</title>
</head>
<body>
<div id="graph">
</div>
<div role="navigation" class="navbar navbar-default navbar-static-top">
<div class="container">
<div class="row">
<div class="col-sm-6 col-md-6">
<ul class="nav navbar-nav">
<li>
<form role="search" class="navbar-form" id="search">
<div class="form-group">
<input type="text" value="JAVAPROJECT" placeholder="Search for Project Title" class="form-control" name="search">
</div>
<button class="btn btn-default" type="submit">Search</button>
</form>
</li>
</ul>
</div>
<div class="navbar-header col-sm-6 col-md-6">
<div class="logo-well">
<a href="http://neo4j.com/developer-resources">
<img src="http://neo4j-contrib.github.io/developer-resources/language-guides/assets/img/logo-white.svg" alt="Neo4j World's Leading Graph Database" id="logo">
</a>
</div>
<div class="navbar-brand">
<div class="brand">Neo4j Company</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-5">
<div class="panel panel-default">
<div class="panel-heading">Search Results</div>
<table id="results" class="table table-striped table-hover">
<thead>
<tr>
<th>Project</th>
<th>TITLE</th>
<th>YEAR</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<div class="col-md-7">
<div class="panel panel-default">
<div class="panel-heading" id="title">Details</div>
<div class="row">
<div class="col-sm-4 col-md-4">
<img src="" class="well" id="poster"/>
</div>
<div class="col-md-8 col-sm-8">
<h4>Programmers</h4>
<ul id="programmers">
</ul>
</div>
</div>
</div>
</div>
</div>
<style type="text/css">
.node { stroke: #222; stroke-width: 1.5px; }
.node.actor { fill: #888; }
.node.movie { fill: #BBB; }
.link { stroke: #999; stroke-opacity: .6; stroke-width: 1px; }
</style>
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
function showProject(title) {
$.get("/project/search/findByTitle?title=" + encodeURIComponent(title), // todo fix paramter in SDN
function (data) {
if (!data ) return; // || !data["_embedded"].project) return;
var project = data; // ["_embedded"].project[0];
$("#title").text(project.title);
$("#poster").attr("src","http://neo4j-contrib.github.io/developer-resources/language-guides/assets/posters/"+encodeURIComponent(project.title)+".jpg");
var $list = $("#programmers").empty();
project.programmer.forEach(function (cast) {
$.get(cast._links.programmer.href, function(programmerData) {
var programmer = programmerData.name;
//var job = cast.job || "IMPLEMENTED";
$list.append($("<li>" + programmer + "</li>"));
});
});
}, "json");
return false;
}
function search() {
var query=$("#search").find("input[name=search]").val();
$.get("/project/search/findByTitleLike?title=*" + encodeURIComponent(query) + "*",
function (data) {
var t = $("table#results tbody").empty();
if (!data) return;
data = data["_embedded"].projects;
data.forEach(function (project) {
$("<tr><td class='project'>" + project.title + "</td><td>" + project.year + "</td></tr>").appendTo(t)
.click(function() { showProject($(this).find("td.project").text());})
});
showProject(data[0].title);
}, "json");
return false;
}
$("#search").submit(search);
search();
})
</script>
<script type="text/javascript">
var width = 800, height = 800;
var force = d3.layout.force()
.charge(-200).linkDistance(30).size([width, height]);
var svg = d3.select("#graph").append("svg")
.attr("width", "100%").attr("height", "100%")
.attr("pointer-events", "all");
d3.json("/graph", function(error, graph) {
if (error) return;
force.nodes(graph.nodes).links(graph.links).start();
var link = svg.selectAll(".link")
.data(graph.links).enter()
.append("line").attr("class", "link");
var node = svg.selectAll(".node")
.data(graph.nodes).enter()
.append("circle")
.attr("class", function (d) { return "node "+d.label })
.attr("r", 10)
.call(force.drag);
// html title attribute
node.append("title")
.text(function (d) { return d.title; })
// force feed algo ticks
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
});
</script>
</body>
</html>
的pom.xml
<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.companie</groupId>
<artifactId>neo4j-companie</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Neo4j Companie</name>
<description>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.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<neo4j-ogm.version>2.1.2</neo4j-ogm.version>
</properties>
<dependencies>
<dependency>
<groupId> org.springframework.data </groupId>
<artifactId> spring-data-neo4j </artifactId>
<version> 3.1.2.RELEASE </version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-bolt-driver</artifactId>
<version>${neo4j-ogm.version}</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-test</artifactId>
<scope>test</scope>
</dependency>
<!-- For use in testing -->
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-embedded-driver</artifactId>
<version>${neo4j-ogm.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>3.1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId> javax.transaction </groupId>
<artifactId> jta </artifactId>
<version> 1.1 </version>
</dependency>
<dependency>
<groupId> javax.validation </groupId>
<artifactId> validation-api </artifactId>
<version> 1.0.0.GA </version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
谢谢!