Repository Layer

Learn how to create a class that represents the domain object and a reactive repository.

Let’s start from the source of our Reactive Data Stream: MongoDB. We need to create a class that represents the domain object, Quote, and a Spring Data Repository to collect and map this data to Java objects.

The Quote class

Below, you can find the Quote class implementation. It just contains an identifier, the book title, and the quote contents.

Press + to interact
package com.thepracticaldeveloper.reactiveweb.domain;
public final class Quote {
private String id;
private String book;
private String content;
// Empty constructor is required by the data layer and JSON de/ser
public Quote() {
}
public Quote(String id, String book, String content) {
this.id = id;
this.book = book;
this.content = content;
}
public String getId() {
return id;
}
public String getBook() {
return book;
}
public String getContent() {
return content;
}
@Override
public String toString() {
return "Quote{" +
"id='" + id + '\'' +
", book='" + book + '\'' +
", content='" + content + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Quote quote = (Quote) o;
if (id != null ? !id.equals(quote.id) : quote.id != null) return false;
if (book != null ? !book.equals(quote.book) : quote.book != null) return false;
return content != null ? content.equals(quote.content) : quote.content == null;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (book != null ? book.hashCode() : 0);
result = 31 * result + (content != null ? content.hashCode() : 0);
return result;
}
}

Reactive repository with Spring 5 and Spring data

Creating a basic reactive repository is as simple as creating a classic one in Spring Data: you just need to create an interface that extends ReactiveCrudRepository, which is the reactive version of CrudRepository ...