garey
garey

Reputation: 67

Jersey client examples in Neo4j Manual

I want to use the Jersey client to connect via REST with a Neo4j database. In the Neo4j manual they have examples of this in Tutorials->Languages->How to use the REST API from Java. I want to create a new node and then use Cypher to add relationships to it. In the Neo4j example (https://github.com/neo4j/neo4j/blob/2.2.9/community/server-examples/src/main/java/org/neo4j/examples/server/CreateSimpleGraph.java) they use 'createNode', but the documentation suggests that that is only available using an embedded Neo4j server.

Does calling createNode() work in a RESTful context?

Upvotes: 0

Views: 310

Answers (1)

William Lyon
William Lyon

Reputation: 8546

In the example that you reference, the createNode function as defined here is simply making an HTTP POST request to http://localhost:7474/db/data/node which will create a new node:

private static URI createNode()
{
    final String nodeEntryPointUri = SERVER_ROOT_URI + "node";
    // http://localhost:7474/db/data/node

    WebResource resource = Client.create()
            .resource( nodeEntryPointUri );
    // POST {} to the node entry point URI
    ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
            .type( MediaType.APPLICATION_JSON )
            .entity( "{}" )
            .post( ClientResponse.class );

    final URI location = response.getLocation();
    System.out.println( String.format(
            "POST to [%s], status code [%d], location header [%s]",
            nodeEntryPointUri, response.getStatus(), location.toString() ) );
    response.close();

    return location;
}

This function is defined in the sample code and is completely different than the createNode function that is part of the embedded Java API.

If you are interested in working with the new Neo4j 3.0 version (currently RC) there is a new Java driver that supports Cypher here.

Upvotes: 2

Related Questions