siliconchris
siliconchris

Reputation: 633

java neo4j creating node with cypher: java.lang.String cannot be cast to java.util.Map

Hi Graphista and other Java gurus,

I have changed my code according to answer given and after inspecting the example provided by neo4j, but still to no avail. So please, could anyone help me with this???

I have a java program which connects to a Neo4J REST API endpoint via jersey client (version 2.11 from org.glassfish.jersey.core).

this is what I do:

        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());

The json in the cypherstring I send to the rest api looks like this:

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

The error message I receive on this is:

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

As you can see, my code is rather simple. I have taken it straight from neo4j website (http://neo4j.com/docs/stable/server-java-rest-client-example.html) - but it always bails out (see log below).

Please see the error log and give me a hint on what I did wrong.

Thanks in advance,

Christian

"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"
  }
}

Upvotes: 1

Views: 1003

Answers (1)

joslinm
joslinm

Reputation: 8105

If your cypherString looks like that, then it's probably wrong. It's trying to parse JSON, and you're not providing it JSON. As you can see from the example, their payload is:

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

You should do the same thing, but replace query with your variable, cypherString. As a result, you would have:

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

Upvotes: 3

Related Questions