java - 非托管扩展 Neo4j 上的密码查询

标签 java neo4j cypher

我的 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 内部错误。 如果我删除代码

Result result = database.execute( "MATCH (n:KISI) where id(n)=1 return n" );

那么一切都很好。

我检查了日志文件,错误如下

Aug 13, 2015 3:34:36 AM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container java.lang.NoSuchMethodError: org.neo4j.graphdb.GraphDatabaseService.execute(Ljava/lang/String;)Lorg/neo4j/graphdb/Result; at org.neo4j.examples.server.unmanaged.HelloWorldResource.hello(HelloWorldResource.java:55) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60) at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205) at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75) at org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:139) at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288) at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469) at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339) at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699) at javax.servlet.http.HttpServlet.service(HttpServlet.java:848) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:698) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:505) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:211) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1096) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:432) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:175) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1030) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136) at org.eclipse.jetty.server.handler.HandlerList.handle(HandlerList.java:52) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) at org.eclipse.jetty.server.Server.handle(Server.java:445) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:268) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:229) at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:358) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:601) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:532) at java.lang.Thread.run(Thread.java:745)

那么我的代码有什么问题吗?

最佳答案

我猜你的 Neo4j 发行版本和 pom.xml 中的 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 文件的构建方式。

  • 所有 Neo4j 依赖项都应在 pom.xml 中提供(Neo4j 发行版中已存在)。
  • 检查您的数据库版本和 pom.xml 中的依赖项版本是否相同。 GraphDatabaseService::execute 方法是最近发明的(如果我没记错的话,2.2.3)。可能您的数据库发行版比您的 Maven 依赖项更旧。

关于java - 非托管扩展 Neo4j 上的密码查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31981593/

相关文章:

collections - Neo4j 中的并集和计数集合

Neo4J、SDN 和运行 Cypher 空间查询

java - 比较时出现问题,使点在单击时改变颜色

java - 不考虑 Mapper 和 Reducer 接口(interface)

java - 如何使我的代码线程安全?

java - neo4j 合并查询使用 REST API 执行

spring-boot - Neo4j 无法将 java.lang.Long 字段 model.Authority.id 设置为 model.Authority

neo4j - Spring Data Neo4j 在访问已检索到的对象时引发 NotInTransactionException

neo4j - 如何通过neo4j cypher中的节点标签过滤结果?

java - 数据输入数据输出 Android 应用程序 - NullPointerException - 需要实现intent.getLongExtra()