Getting started with the Neo4j Java Rest Binding (Heroku deployment)

Believe it or not, the last couple of days were my first attempt at using Neo4j’s Java Rest Binding. My earlier application on Heroku, Flavorwocky, used the Grails Neo4j plugin and for other stuff including work, embedded Neo4j rules.

However, embedded won’t do when deploying to Heroku, so the Java Rest Binding it is. Much better than dealing with raw REST, especially if you are very used to the embedded api’s.

Since it took me a short while to figure things out, mostly due to my shameful lack of Maven knowledge, thought I’d post a simple “getting started” guide.

At the end of this, your application and Neo4j should be talking to each other.

Note 1: This article deals with the rest binding for Neo4j 1.8.1, which is the version used by my Heroku application.

Note 2: Add the Neo4j add-on to your Heroku application if you do plan on trying this out

Step 1: Get/Include the library

If you are not deploying to Heroku and/or want to just include the jars, pick them up from here:

If you like/have to use maven, include the following dependency in your pom.xml


The dependency refused to resolve until I added the following:


Step 2: Connect to your local Neo4j server

I created a simple servlet to test code and figure out the library, you might add this code to whatever you like.

RestAPI graphDb = new RestAPIFacade("http://localhost:7474/db/data");

Note that you can also use

GraphDatabaseService graphDb=new RestGraphDatabase(“http://localhost:7474/db/data”);

which is what I attempted to use first, and it does work, but doesn’t seem to be a valid parameter when creating a RestCypherQueryEngine (see step 3)

Step 3: Does it work?

Execute a Cypher query to return the number of nodes in the database.

QueryEngine engine=new RestCypherQueryEngine(graphDb);  
 QueryResult<Map<String,Object>> result=  
     engine.query("start n=node(*) return count(n) as total", Collections.EMPTY_MAP);  
 Iterator<Map<String, Object>> iterator=result.iterator();  
 if(iterator.hasNext()) {  
   Map<String,Object> row=;  
   out.print("Total nodes: " + row.get("total"));  

Step 4: Let’s find out

Now is a good time to start your Neo4j server if you haven’t already.

If you’re using Tomcat and following the guide here then build and run:

mvn package  
 sh target/bin/webapp

or launch your web application otherwise.

Access your servlet/jsp/anything else and you should see the count of nodes in your Neo4j database. A new database will return 1 node (the reference node)

Step 5: Deploy to Heroku

The URL to your Neo4j database on Heroku can be found in the NEO4J_URL environment variable so you don’t need to hardcode this in your application.

The variable value looks like this:

Pick up the variable in your application code using

String neoUrl=System.getenv("NEO4J_URL");

and parse it to extract the username, password and db URL (

Modify your code to now accept a username and password:

GraphDatabaseService graphDb=new RestGraphDatabase(dbUrl + “/db/data”,username,password);

Push changes to heroku, access your application, and you should see the count of nodes in your remote instance.

Step 6: Add a node just to be really sure

Transaction tx = graphDb.beginTx();  
 Map<String,Object> props=new HashMap<String, Object>();  
 Node node=graphDb.createNode(props);  

Execute the same Cypher query to fetch the count of nodes and you should see it increment by 1.

If you can’t wait to see the REST calls that the binding is actually producing, you can turn on logging. As per the documentation, you need to set the system property to true. mI saw the REST calls being logged on my Tomcat console.

Further googling told me that I could set this property on Heroku by adding it  as an extraJvmArgument in the appassembler-maven-plugin in pom.xml:


That’s it! You should now be able to manipulate your graph using Cypher or the core API, but more on transactions and batching in Part 2.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

2 Responses to "Getting started with the Neo4j Java Rest Binding (Heroku deployment)"

  1. Thanks a lot Luanne. That was a really useful post. Found exactly what I was looking for.

  2. kalyan says:

    Please implement a graph database (a kind of NoSQL). This graph database
    should consist of nodes (with have properties) for entities and edges (which
    have single or multiple properties and can be directional or bidirectional) for
    relationships, and support node indexing and query. The query language has
    following keywords: START, MATCH, WHERE, RETURN, ORDER BY,

    Example Input:
    Node martin = graphDB.createNode();
    martin.setProperty(“name”, “Martin”);
    Node pramod = graphDB.createNode();
    pramod.setProperty(“name”, “Pramod”);
    Node barbara = graphDB.createNode();
    pramod.setProperty(“name”, “Barbara”);
    martin.createRelationshipTo(pramod, FRIEND, since = 1998);
    pramod.createRelationshipTo(martin, FRIEND, since = 1998);
    martin.createRelationshipTo(barbara, FRIEND);
    barbara.createRelationshipTo(martin, FRIEND);
    START node = Node:nodeIndex(name = “Barbara”)
    MATCH path = node-[relation*1..3]->related_node
    WHERE type(relation) = ‘FRIEND’
    RETURN, length(path), relation.since;

Leave a Reply

seven − = 2

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books