我的 neo4j 服务器有一个非托管扩展。
和如下代码。
@Path("/helloworld")
public class HelloWorldResource {
private final GraphDatabaseService database;
public HelloWorldResource(@Context GraphDatabaseService database) {
this.database = database;
}
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/{nodeId}")
public Response hello(@PathParam("nodeId") long nodeId) {
String res = "";
try ( Transaction ignored = database.beginTx();)
{
//@@problem
Result result = database.execute( "MATCH (n:KISI) where id(n)=1 return n" );
} catch (Exception e) {
res = "Error = " + e.getMessage();
}
return Response
.status(Status.OK)
.entity(("nodeId =" + nodeId + " " + res).getBytes(Charset
.forName("UTF-8"))).build();
}
}
Run Code Online (Sandbox Code Playgroud)
当我部署代码时,出现 500 内部错误。如果我删除代码
结果 result = database.execute( "MATCH (n:KISI) where id(n)=1 return n");
那么一切都很好。
我检查了日志文件,错误如下
2015 年 8 月 13 日上午 3:34:36 com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException 严重:MappableContainerException 中包含的异常无法映射到响应,重新抛出到 HTTP 容器 java.lang.NoSuchMethodError: org.neo4j.graphdb.GraphDatabaseService.execute(Ljava/lang/String;)Lorg/neo4j/graphdb/Result; 在 org.neo4j.examples.server.unmanaged.HelloWorldResource.hello(HelloWorldResource.java:55) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun .reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1。
那么我的代码有什么问题吗?
我猜你的 Neo4j 发行版和 pom.xml 中的 maven 依赖版本不一样。
但是有几件事需要检查:
1)您应该始终关闭Result
对象。例子:
try(Result result = database.execute( "MATCH (n:KISI) where id(n)=1 return n" )) {
// do stuff here
}
Run Code Online (Sandbox Code Playgroud)
``
2) 异常不是发生在try-catch
而是在之后。您应该将代码更改为:
try ( Transaction tx = database.beginTx()) {
String query = "MATCH (n:KISI) where id(n)=1 return n";
// use result with try-with-resource to ensure that it will be closed
try(Result result = database.execute(query)) {
// do stuff you need with result here
return Response.ok("nodeId =" + nodeId).build();
}
tx.success(); // mark transaction as successful
} catch (Exception e) {
// If exception occurs - send exception message with 500 status code
// It's good idea to write Exception stacktrace to log there
return Response.serverError().entity(e.getMessage()).build()
}
Run Code Online (Sandbox Code Playgroud)
3) 您应该检查非托管扩展.jar
文件是如何构建的。
provided
在 pom.xml 中(Neo4j 发行版中已经有了)。GraphDatabaseService::execute
方法是最近发明的(如果我没记错的话,是 2.2.3)。可能您的数据库发行版比您的 Maven 依赖项更旧。