我在localhost上运行了neo4j 2.3.X,使用以下命令连接到API:
所有这些都在java-8-oracle JVM上运行
我在windows和linux上测试了这个问题,同样的问题。
当我在api上执行GET请求时,每个端点(我测试了所有端点),neo4j-ogm引发了MappingException:
2016-02-24 09:52:59.821 ERROR 7496 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.neo4j.ogm.exception.MappingException: Error mapping GraphModel to instance of com.bnstips.api.database.model.BnSClass] with root cause
java.lang.NullPointerException: null
at org.neo4j.ogm.session.Utils.coerceTypes(Utils.java:80) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
at org.neo4j.ogm.annotations.FieldWriter.write(FieldWriter.java:69) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
at org.neo4j.ogm.context.GraphEntityMapper.tryMappingAsSingleton(GraphEntityMapper.java:221) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
at org.neo4j.ogm.context.GraphEntityMapper.mapRelationship(GraphEntityMapper.java:258) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
at org.neo4j.ogm.context.GraphEntityMapper.mapRelationships(GraphEntityMapper.java:245) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
at org.neo4j.ogm.context.GraphEntityMapper.mapEntities(GraphEntityMapper.java:143) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:117) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:81) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
at org.neo4j.ogm.session.delegates.LoadByTypeDelegate.loadAll(LoadByTypeDelegate.java:73) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
at org.neo4j.ogm.session.delegates.LoadByTypeDelegate.loadAll(LoadByTypeDelegate.java:110) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
at org.neo4j.ogm.session.Neo4jSession.loadAll(Neo4jSession.java:97) ~[neo4j-ogm-core-2.0.0-M02.jar:na]
at com.bnstips.api.database.GenericService.findAll(GenericService.java:18) ~[classes/:na]
at com.bnstips.api.endpoint.ClassEndpoint.getClasses(ClassEndpoint.java:54) ~[classes/:na]
...
我在我自己的代码所在的第一行停止了堆栈跟踪副本,但如果需要,我可以复制整个跟踪。
我的项目就像第一个例子一样完成:http://neo4j.com/docs/ogm/java/stable/
所以我的所有服务都通过CRUD操作扩展了GenericService<T>
。
以下是ClassEndpoint.getClasses(long id)
实施:
@RequestMapping(method=RequestMethod.GET)
@ResponseBody
public Iterable<BnSClass> getClasses(){
Iterable<BnSClass> classes = this.service.findAll();
classes.forEach((c) -> {
c.createLinks();
});
return classes;
}
和findAll方法:
@Override
public Iterable<T> findAll() {
return session.loadAll(getEntityType(), DEPTH_LIST);//DEPTH_LIST is -1
}
最后,Class
模型:
@NodeEntity(label="Class")
public class BnSClass extends Entity<BnSClass>{
private String name;
@Relationship(type="TIP_FOR", direction="INCOMING")
@JsonIgnore private Set<Tip> tips = new HashSet<Tip>();
@Relationship(type="USED_BY", direction="INCOMING")
@JsonIgnore private Set<Build> builds = new HashSet<Build>();
...Getters and Setters ommited
}
我的数据库非常简单,我可以通过简单的屏幕截图向您展示:
:Class
个节点只使用一个属性:name
。
编辑:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.6.0</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-api</artifactId>
<version>2.0.0-M02</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-core</artifactId>
<version>2.0.0-M02</version>
</dependency>
</dependencies>
编辑2:
经过一些尝试,我设法摆脱了错误,但为此我必须将DEPTH_ENTITY设置为1但它只修复findAll()
个查询,所以这不是一个解决方案,但它可能会找到一种方法来找到解决方案
答案 0 :(得分:0)
好的,我找到了它的来源,让我解释一下:
BnSClass
延伸Commentable<T>
,此处为Commentable<T>
:
@NodeEntity
public abstract class Commentable<T> extends Rateable<T>{
@Relationship(type="COMMENTED", direction="INCOMING")
@JsonIgnore private Set<Comment> comments = new HashSet<Comment>();
public Set<Comment> getComments(){
return this.comments;
}
}
如您所见,Commentable<T>
为我的对象添加了一组Comment
,问题来自Comment
:
@NodeEntity
@JsonPropertyOrder({"id", "content", "score"})
public class Comment extends Commentable<Comment> implements CreationDate{
@Relationship(type="COMMENTED", direction="OUTGOING")
@JsonIgnore private Entity<?> subject;
private String content;
private long edited;
private boolean deleted = false;
@JsonIgnore private Authored authored;
}
如果我将subject
类型设置为Entity
rawtype,它就像魅力一样,但如果我将Entity<?>
设为类型,则会产生此错误。由于我的api中的几乎所有资源都是可评论的或具有Set,因此每次从数据库中获取时都会发生此错误。
N.B:每个节点实体都继承自Entity<T>
,这是neo4j-ogm指南中提供的Entity<T>
类。