Last Updated: February 25, 2016
· mab

Simple cypher request through the neo4j rest api

Here is a simple example for posting a cypher request through the neo4j rest api with jersey.

package de.devmb.playground.neo4j.hellorest;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.LoggingFilter;

public class HelloRest {

  public static String SERVER_ROOT_URI = "http://localhost:7474/db/data/";

  public static void main(String[] args) {
    Client client = Client.create();
    client.addFilter(new LoggingFilter(System.out));
    WebResource cypher = client.resource(SERVER_ROOT_URI + "cypher");
    String request = "{\"query\":\"MATCH (n) RETURN n\"}";
    ClientResponse cypherResponse = cypher.accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, request);

It is important to set the MediaType. To encapsulate the JSON have look at the GSON Library..