Reactive programming with Neo4j

Kickstart a Spring Boot project by Spring Initializr.

  • Project type: Maven
  • Java version : 11
  • Spring Boot : 2.2.0.M5
  • Dependencies: Reactive Web, Lombok

Do not add Spring Data Neo4j into the dependencies, we will use the new Spring Data Neo4j Rx instead.

Do not forget to add Lombok to your project dependencies. We’ll use Lombok to get the Java codes clean as possible, esp. to generate the getters and setters, hashCode, equals, toString for you at compile time.

Download the generated project skeleton archive and extract the files into your machine.

Open the pom.xml file in the project root folder, add the Spring Data Neo4j RX dependency from the Neo4j team manually.


Declare the property spring-data-neo4j-rx.version in properties.


Now let’s add some codes to experience the new Reactive features in Spring Data Neo4j Rx.

Create a POJO class for presenting a node in Neo4j graph.

class Post {

In the above codes, @Node, @Id, @GeneratedValue are located in the package which is part of the new Spring Data Neo4j Rx project.

Spring Data Neo4j Rx also supports the annotations from Spring Data Commons project. For example, you can use Id and Persistent from Spring Data Commons instead of the Id and Node annotations from the new Spring Data Neo4j RX.

Spring Data Neo4j Rx supports data auditing as well, so you can use @CreatedDate, @CreatedBy, @LastModifiedDate, @LastModifiedBy as usually, just add @EnableNeo4jAuditing on the application class to enable it.

Create a Repository class for Post.

interface PostRepository extends ReactiveNeo4jRepository<Post, Long> {

Create a Controller to perform CRUD operations.

@RequestMapping(value = "/posts")
class PostController {

The above code is easy to understand, the main difference is here it returns Reactor’s Flux or Mono type. In the get method, if the Post is not found, throw an exception PostNotFoundException in the reactive flow by Mono.error.

class PostNotFoundException extends RuntimeException {

When the PostNotFoundException is caught, send a 404 Not Found status code to the client.

class RestExceptionHandler {

Almost done. Let’s try to initialize some data for demo purpose.

Create a CommandLineRunner bean to insert some data.

class DataInitializer implements CommandLineRunner {

Benefit from the Spring Boot auto-configuration mechanism, a ReactiveNeo4jClient bean is ready for use. You can use it to interact with the Neo4j low level APIs, such as executing Cypher queries.

To experience the new reactive features, you have to use Neo4j 4.0 which is still under development.

The following is a docker-compose configuration which allow you run Neo4j server in docker containers.

image: neo4j/neo4j-experimental:4.0.0-alpha09mr02-enterprise # not release yet.
- "NEO4J_AUTH=neo4j/test"
- 7687:7687
- 7474:7474

Configure the Neo4j driver properties in file.


Start up the application, use curl to test the APIs.

# Get all posts
curl http://localhost:8080/posts
[{"id":0,"title":"Post one","content":"content of Post one","createdDate":null},{"id":1,"title":"Post two","content":"content of Post two","createdDate":null}]

Get the codes from my Github.

Written by

Self-employed technical consultant, solution architect and full-stack developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store