我的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();
}
}
当我部署代码时,我得到了500个内部错误。如果我删除代码
结果结果= database.execute(“ MATCH(n:KISI)其中id(n)= 1返回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(本机方法)在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) .reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at com.sun.jersey.spi.container.JavaMethodInvokerFactory $ 1.invoke(JavaMethodInvokerFactory.java:java.lang.reflect.Method.invoke(Method.java:606) 60),位于com.sun.jersey.server。
那我的代码怎么了?
我猜您pom.xml中的Neo4j发行版本和maven依赖版本不相同。
但是有几件事要检查:
1)您应该始终关闭Result
对象。例子:
try(Result result = database.execute( "MATCH (n:KISI) where id(n)=1 return n" )) {
// do stuff here
}
```
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()
}
3)您应该检查非托管扩展.jar
文件的构建方式。
provided
位于pom.xml中(Neo4j发行版中已经存在)。GraphDatabaseService::execute
方法是最近发明的(如果我没记错的话,是2.2.3)。可能您的数据库发行版比您的Maven依赖项还旧。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句