Reactive Revolution — MongoDB

The Reference from: https://app.pluralsight.com/library/courses/allthetalks-session-68/table-of-contents

In this example, we’re making the use of Reactive Web, Reactive MongoDB and Embeded MongoDB.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-rsocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>

MainApp.java

package com.example.service;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.EventListener;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import static org.springframework.web.reactive.function.server.RouterFunctions.route;

@SpringBootApplication
public class ServiceApplication {

public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}


@Bean
public RouterFunction<ServerResponse> routes(ReservationRepository rr) {
return route()
.GET("/reservations", request -> ServerResponse.ok().body(rr.findAll(), Reservation.class))
.build();
}

}

@Component
class SampleDataInit {
@Autowired
private ReservationRepository reservationRepository;

@EventListener(ApplicationReadyEvent.class)
public void go() {
Flux<Reservation> reservationFlux = Flux.just("Neha", "Aravind", "Laxmi", "Rajesh", "Anosh", "Sam", "Alex")
.map(name -> new Reservation(null, name))
.flatMap(r -> this.reservationRepository.save(r));

this.reservationRepository.deleteAll()
.thenMany(reservationFlux)
.thenMany(this.reservationRepository.findAll())
.subscribe(System.out::println);

}
}

interface ReservationRepository extends ReactiveCrudRepository<Reservation, String> {

}


@AllArgsConstructor
@NoArgsConstructor
@Data
@Document
class Reservation {
@Id
private String id;
private String name;
}

hit the URL: http://localhost:8080/reservations

[{"id":"6229f519286f8b70005b92f3","name":"Neha"},{"id":"6229f519286f8b70005b92f4","name":"Aravind"},{"id":"6229f519286f8b70005b92f5","name":"Laxmi"},{"id":"6229f519286f8b70005b92f8","name":"Sam"},{"id":"6229f519286f8b70005b92f7","name":"Anosh"},{"id":"6229f519286f8b70005b92f6","name":"Rajesh"},{"id":"6229f519286f8b70005b92f9","name":"Alex"}]

HTTP URL: http://localhost:8080/reservations

[{"id":1,"name":"Neha"},{"id":2,"name":"Aravind"},{"id":3,"name":"Laxmi"},{"id":4,"name":"Rajesh"},{"id":5,"name":"Anosh"},{"id":6,"name":"Sam"},{"id":7,"name":"Alex"}]

--

--

--

Java Developer and enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Become an expert on Copado

Lessons Learned from Our First Adventure in Software Engineering

How to download specific folders on Github

The pros and cons of using AWS Cognito for user authentication in web and mobile apps

Synerise People: Hackathon Case — Having fun While Creating an Application on a Deadline

How to deploy FastAPI with Nginx and Supervisor

Planning to Learn SQL from Scratch

An apple balancing on textbooks.

Understanding the state of a project in 30 seconds or less

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
PA

PA

Java Developer and enthusiast

More from Medium

Java I/O Character Stream Implementation

Develop Bluetooth LE application easily with JAVA and BleuIO

Hide a Specific Paragraph in Word Using Java

ProjectReactor — Handle