Normally, we will use Spring MVC for our project, but Spring webflux was born in 2017, it makes our web application stronger and more elastic. It takes advantage of asynchronous property, non-blocking IO to reduce the number of threads when receiving multiple requests.

So, in this article, we will move on Spring Webflux and MongoDB to work. Let’s get started.

Table of contents

Why we choose Mongodb for this Spring webflux project

Actually, we want to choose MySQL for spring webflux, because MySQL is popular, and its document is really helpful, and some interesting tutorials about it.

But the reason that we do not select MySQL is that there is no reactive driver for MySQL.

It has reactive driver for PostgreSQL, Mongodb. So, we will choose Mongodb to do with Spring webflux.

Some necessary packages for this project






Belows is the means of some above packages:

  • spring-boot-starter-webflux is spring webflux, the reactive version of Spring MVC.
  • spring-boot-starter-test and reactor-test is some packages for testing.
  • spring-boot-starter-data-mongodb-reactive is the reactive MongoDB dependency that we need to communicate with MongoDB asynchronously.
  • de.flapdoole.embed.mongo is an embedded, in-memory MongoDB database.

Configure information of MongoDB

Below is the configuration of file. Spring Boot will automatically use this file to configure our application.

# = mongodb://<user>:<passwd>@<url>:<port>/<dbname>

If we use Mongo 2.x, we can specify a host/port in = mongoserver = 27017

If we use Mongo 3.0 Java driver, and are not supported. In such cases, should be used to provide all of the configuration.

To know more information about MongoDB, we can refer this link.

All available options for prefix are fields of MongoProperties:

private String host;

private int port = DBPort.PORT;

private String uri = "mongodb://localhost/test";

private String database;

private String gridFsDatabase;

private String username;

private char[] password;

Create database in MongoDB

In order to start MongoDB in command line, we need to install MongoDB application, and access to the path in Windows: C:\Program Files\MongoDB\Server\4.0\bin. At this path, type Alt + D and cmd.

Next, typing mongod to start this application.

To know more about operations with MongoDB in command line, we can refer to this link.

show dbs;
use webflux-mongodb;

// display all tables
show collections;

// create collection employees.

// insert data into our collection
db.employees.insert({"id": "5d753f4c0b729b259d72061b", "name": "Obama", "address": "California", "phone": "012541425541", "salary": 500000});
db.employees.insert({"id": "512ad753f4c0b729b259d720", "name": "Clinton", "address": "New York", "phone": "012541421541", "salary": 250000});

Source code for this project

We can refer webflux-webservice.

Wrapping up

  • We know about how to use Spring webflux with maven project.
  • To know more about some DBMSs that support asynchronous programming.

Thanks for your reading.