1

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"
  }
}
3
  • can you share your cypher statement and the URL that you are posting to? Commented Nov 13, 2014 at 0:47
  • yes, sure. my cypher statement is this: {"statements": [ {"statement": "CREATE" (p:"POST" {"id":"532769886335168512","text":"my text","lang":"de"}) }]} and I'm sending it to endpoint localhost:7474/db/data/transaction. the json-string for the attributes is created with the toJsonString() method, and I add the statements-part together with corresponding parentheses before sending it over the wire. Commented Nov 13, 2014 at 21:14
  • Any chance, anyone can help me here??? I'm still completely stuck. I now use the transaction endpoint and my cypher-statement is, as you can see, according to the examples given by neo4j, but still I receive this error: Neo.ClientError.Request.InvalidFormat","message":"Unable to deserialize request: Unexpected character ('(' (code 40)): was expecting comma to separate OBJECT entries Commented Nov 17, 2014 at 20:16

1 Answer 1

3

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 );
Sign up to request clarification or add additional context in comments.

6 Comments

Ok, maybe I'm somewhat stupid here, but after changing my cypher statement, I receive a different error. This is my new cypher statement: {"CREATE" : [ {"s" : "POST" {"id":"532466871140753408","text":"hier ist der text","lang":"de"}} ]}. This is the new error: "java.lang.String cannot be cast to java.util.Map",
I don't know what you're doing, but you need a structure that looks like this { "statements": [ {"statement": "CREATE (p:Post {id: 523, text: "hier ist der text", lang: "de"}) RETURN p ] } Neo4j will parse the JSON for the cypher query(s).
I have edited my statement as below: {"statements": [ {"statement": "CREATE" (p:"POST" {"id":"532555943180263424","text":"mein text","subject":"mein subject","teaser":"mein teaser","lang":"de"})}]} but still the same result
you're sending it to the wrong endpoint, I think. Make sure you're sending the post to the transactional cypher endpoint URL, not the node endpoint.
Actually the " around the keys are automatically generated by the toJsonString() method. But nevertheless, you are right. It's my first time with neo4j, so I expected a learning curve :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.