java neo4j用cypher创建节点:java.lang.String不能强制转换为java.util.Map

时间:2014-11-11 21:59:51

标签: java json rest neo4j jersey

嗨Graphista和其他Java专家,

我根据给出的答案更改了我的代码,并在检查了neo4j提供的示例之后,但仍无济于事。所以,请有人帮我这个???

我有一个java程序,它通过jersey客户端(来自org.glassfish.jersey.core的2.11版本)连接到Neo4J REST API端点。

这就是我的所作所为:

        logger.trace("sending cypher {} to endpoint {}", cypherString, nodePointUrl);
        WebResource resource2 = Client.create().resource( nodePointUrl );

        ClientResponse response2 = resource2
                .accept( "application/json" )
                .type( "application/json" )
                .entity( cypherString )
                .post( ClientResponse.class );

        logger.debug("POST to {} returned status code {}, returned data: {}",
                nodePointUrl, response2.getStatus(),
                response2.getEntity(String.class));

        HttpStatusCodes httpStatusCodes = HttpStatusCodes.getHttpStatusCode(response2.getStatus());

我发送到其余api的cypherstring中的json看起来像这样:

{"CREATE": [{"POST": {"id":"532552232906940416","text":"Warburg Research...","subject":"Warburg Research ...","teaser":"Warburg Research...","lang":"de"}}]}

我收到的错误信息是:

 java.lang.String cannot be cast to java.util.Map

如您所见,我的代码非常简单。我从neo4j网站(http://neo4j.com/docs/stable/server-java-rest-client-example.html)直接采取了 - 但它总是挽救(见下面的日志)。

请查看错误日志,并提示我做错了什么。

提前致谢,

基督教

"message" : "java.lang.String cannot be cast to java.util.Map",
"exception" : "BadInputException",
"fullname" : "org.neo4j.server.rest.repr.BadInputException",
"stacktrace" : [ "org.neo4j.server.rest.repr.formats.JsonFormat.readMap(JsonFormat.java:92)", "org.neo4j.server.rest.web.RestfulGraphDatabase.createNode(RestfulGraphDatabase.java:238)", "java.lang.reflect.Method.invoke(Method.java:606)", "org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:139)", "java.lang.Thread.run(Thread.java:745)" ],
"cause" : {
   "message" : "java.lang.String cannot be cast to java.util.Map",
   "exception" : "ClassCastException",
   "stacktrace" : [ "org.neo4j.server.rest.domain.JsonHelper.jsonToMap(JsonHelper.java:53)", "org.neo4j.server.rest.repr.formats.JsonFormat.readMap(JsonFormat.java:88)", "org.neo4j.server.rest.web.RestfulGraphDatabase.createNode(RestfulGraphDatabase.java:238)", "java.lang.reflect.Method.invoke(Method.java:606)", "org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:139)", "java.lang.Thread.run(Thread.java:745)" ],
  "fullname" : "java.lang.ClassCastException"
  }
}

1 个答案:

答案 0 :(得分:3)

如果您的cypherString看起来像那样,那么它可能就错了。它试图解析JSON,而你却没有提供JSON。从示例中可以看出,它们的有效负载是:

String payload = "{\"statements\" : [ {\"statement\" : \"" +query + "\"} ]}";

您应该做同样的事情,但用您的变量cypherString替换查询。因此,您将拥有:

String payload = "{\"statements\" : [ {\"statement\" : \"" + cypherString + "\"} ]}";
ClientResponse response = resource
        .accept( MediaType.APPLICATION_JSON )
        .type( MediaType.APPLICATION_JSON )
        .entity( payload )
        .post( ClientResponse.class );