May 18, 2022

Blog @ Munaf Sheikh

Latest news from tech-feeds around the world.

Intro to Spring Data MongoDB Reactive and How to Move It to the Cloud

Great post from our friends at Source link

In this post, we’re going to see how to configure and implement database operations using Reactive Programming through Spring Data Reactive Repositories with MongoDB to run locally and then see how to move it smoothly to the cloud through 

Reactive programming is a programming paradigm that promotes an asynchronous, non-blocking, event-driven approach to data processing. Reactive programming involves modeling data and events as observable data streams and implementing data processing routines to react to the changes in those streams.

To illustrate Reactive MongoDB, we’ll create an application to handle and store the goods of Greek mythology. In order to use Reactive MongoDB, we need to add the dependency to our pom.xml.

The next step is to configure the infrastructure connection, we need to use the  @EnableReactiveMongoRepositories  alongside with some infrastructure setup:

We’ll create a God entity and then annotated it with @Document to use it in the database operations.

The next step is to create a Repository interface. The goal of the Spring Data repository abstraction is to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores. Now with the Reactive model, we get the same set of methods and specifications, except that we’ll deal with the results and parameters in a reactive way.

Besides the repositories approach, there is the ReactiveMongoTemplate where we won’t cover it.

The last step is the controller; what changes is the return in the methods.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public class GodController {

    private GodRepository repository;

    @ResponseStatus(code = HttpStatus.CREATED)
    public Mono<String> save(@RequestBody God god) {
        return -> "Saved: " + g.getName());

    @GetMapping(value = "/{id}", produces = "application/json")
    public Mono<God> get(@PathVariable("id") String id) {
        return repository.findById(id);

    @GetMapping(produces = "application/json")
    public Flux<God> get() {
        return repository.findAll();

    @PutMapping(value = "/{id}", produces = "application/json")
    public Mono<God> update(@PathVariable("id") long id, @RequestBody God god) {;
        return Mono.just(god);

    @DeleteMapping(value = "/{id}", produces = "application/json")
    public Mono<Void> delete(@PathVariable("id") String id) {
        return repository.deleteById(id);

Done, we have the code ready to test. You can either install it locally or run it by Docker. Feel free to pick anyone and test it.

The Java application is ready to go! Let’s move it to the cloud easily with is a second-generation Platform-as-a-Service built especially for continuous deployment.

It allows you to host web applications on the cloud while making your development and testing workflows more productive.

As a Platform as a Service, or PaaS, automatically manages everything your application needs in order to run. That means you can, and should, view your infrastructure needs as part of your application, and version-control it as part of your application.

Every application you deploy on is built as a virtual cluster, containing a set of containers. There are three types of containers within your cluster:

  • One Router (.platform/routes.yaml). allows you to define the routes.
  • Zero or more service containers (.platform/services.yaml). allows you to completely define and configure the topology and services you want to use on your project.
  • One or more application containers ( You control your application and the way it will be built and deployed on via a single configuration file.

Locally, we don’t need to worry about the host, password, user, and so on. We’ll overwrite these properties with the MongoDB information that will instantiate and manage to us.

The application is now ready, so it’s time to move it to the cloud with using the following steps:

  • Create a new free trial account.
  • Sign up with a new user and password, or login using a current GitHub, Bitbucket, or Google account. If you use a third-party login, you’ll be able to set a password for your account later.
  • Select the region of the world where your site should live.
  • Select the blank template.

You have the option to either integrate to GitHub, GitLab, or will provide to you. Finally, push to the remote repository:

Done! We have a simple and nice Spring Webflux application ready to go to the cloud.

#Intro #Spring #Data #MongoDB #Reactive #Move #Cloud