Last Updated: February 25, 2016
·
3.011K
· 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 javax.ws.rs.core.MediaType; 
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);
    cypherResponse.close();
    System.out.println(cypherResponse);
  }
}

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