How to start with your first Spring Reactor Project

Reading Time: 3 minutes

Hi, In this blog, we will introduce Spring Reactor which you can kickstart using the given example and will be able to understand what are Mono and Flux which are basic building blocks of the streams used by Project Reactor. So let’s Begin!

Making the Spring Boot Project with Maven

We will be using one of the many Maven archetypes to create a sample project for our example. To create the project execute the following command in a directory that you will use as a workspace:

Creating a Project

mvn archetype:generate -DgroupId=com.javacodegeeks.example -DartifactId=JCG-BootReactor-Example -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

If you are running maven for the first time, it will take a few seconds to accomplish the generate command because maven has to download all the required plugins and artifacts in order to make the generation task. Once we run this project, we will see the following output and the project will be created:

Once you have created the project, feel free to open it in your favorite IDE. The next step is to add appropriate Maven Dependencies to the project. We will work with the following dependencies in our project:

Spring-boot-starter-web: This dependency marks this project as a Web project and it adds dependencies in order to create controllers and make Web-related classes.
Reactor-bus: This is the dependency that brings all the Reactor related dependencies into the project classpath.
Spring-boot-starter-test: This dependency collects all test-related JARs into the project like JUnit and Mockito.
Here is the pom.xml file with the appropriate dependencies added:


  </dependency>  <dependency>

Producing a stream of Data

In order for an application to be reactive, we must have a constant flow of data to be our first logical step. Without this data, the application will not have anything to react to.

The reactive core provides us with two data types that allow us to generate a stream of data.

  • Flux
  • Mono


Using Flux we can have a stream of data that can produce 0…n elements.

Flux<Integer> streamOfIntegers = Flux.just(1, 2, 3, 4, 5);

In this case, we have a static stream of 5 elements.


Using Mono, we can produce a stream of 0..1 element at a time.

Mono<Integer> streamOfIntegers = Mono.just(1);

here we are limited to only 1 element in the produced stream.

Collecting elements

We will use the subscribe() method to collect the data from the stream. See the example given below.

List<Integer> elements = new ArrayList<>();
Flux.just(1, 2, 3, 4, 5)

assertThat(elements).containsExactly(1, 2, 3, 4, 5);

Flux.just(1, 2, 3, 4, 5)
.subscribe(new Subscriber<Integer>() {
    public void onSubscribe(Subscription s) {

    public void onNext(Integer integer) {

    public void onError(Throwable t) {}

    public void onComplete() {}


With the above example, you can simply get an overview of what are the building blocks of a reactive application using a spring reactor. In the next blog of this series, we will talk about building this application from end to end. Thanks for Reading!



Written by 

Hey, I am a software consultant at Knoldus working on Java and Functional Programming.