SpringData Reactive MongoDB Repositories | SpringBoot

Reactive programming is about non-blocking applications. With the previous post, we had learned how to use SpringBoot WebFlux. In the tutorial, JavaSampleApproach will explore more about SpringData Reactive MongoDB Repositories with a set of high-level abstraction CRUD methods.

Related posts:
SpringBoot WebFlux Annotation-based RestAPIs
SpringBoot WebFlux Functional RestAPIs
SpringData Reactive Cassandra Repositories | SpringBoot
Angular 4 + Spring Boot + MongoDB CRUD example
– Full Reactive: Angular 4 + Spring WebFlux + Spring Data Reactive MongoDB example

I. Technologies

– Java: 1.8
– Maven: 3.3.9
– Spring Tool Suite: Version 3.9.0.RELEASE
– Spring Boot: 2.0.0.M4
– Spring Boot Starter Webflux
– Spring Boot Starter Data Mongodb Reactive
– MongoDb – v3.4.9

II. SpringData Reactive MongoDb

MongoDB Reactive SpringData provides repositories with a set of high-level abstraction CRUD methods or templates for working. In the tutorial, we focus on Reactive MongoDB repositories implementation.

For working with ReactiveMongodb, we need dependency:

How to work?

We use AbstractReactiveMongoConfiguration to configure Reactive MongoDB database:

We use ReactiveCrudRepository to define reactive-repositories:

We can pass parameters to query methods as plain (e.g. String), wrapped (Optional, Stream) or reactive wrapped arguments (Mono, Flux).
When using reactive wrappers (Mono, Flux) parameters, query creation and execution will be defered until having an actual subscription.

We can combine Spring Web Reactive and SpringData Reactive in a reactive processing way:

III. Practice

We create a SpringBoot project as below:

springdata mongodb reactive - project structure

Step to do:
– Create SpringBoot project
– Create data model
– Configure MongoDb Reactive
– Implement Reactive repository
– Implement WebFlux RestAPIs
– Implement initial client
– Run and check results

1. Create SpringBoot project

Using SpringToolSuite to create SpringBoot project, then add needed dependencies:

springdata mongodb reactive - add needed dependencies

Check pom.xml file after finishing:

2. Create data model

Create Customer data class:

3. Configure MongoDb Reactive

4. Implement Reactive repository

5. Implement WebFlux RestAPIs

Open application.properties file to add a configuration for Pretty-print JSON responses:

6. Implement initial client

7. Run and check results

Build and run the SpringBoot project with commandlines {mvn clean install, mvn spring-boot:run}

-> MongoDb’s documents:

springdata mongodb reactive - all customer in mongo db

– Make a request to get all customers http://localhost:8080/api/customer

springdata mongodb reactive - get all request

– Make a request to get a customer by id http://localhost:8080/api/customer/2

springdata mongodb reactive - get by id

– Make a request to get list full name of customers by age http://localhost:8080/api/customer/findbyage?age=24

springdata mongodb reactive - get by age

– Make a request to get a Customer by full name http://localhost:8080/api/customer/findbynames?firstname=Skyler&lastname=White

springdata mongodb reactive - find by names

IV. Sourcecode


By grokonez | September 23, 2017.

Last updated on March 16, 2018.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *