Spring Reactive APIs with MongoDB
Dr. Arul Xavier V M
MongoDB Database
MongoDB is a another well-known NoSQL database.
MongoDB is considered a document database.
MongoDB stores documents in BSON (Binary JSON) format, which
can be queried for and retrieved in a way that’s roughly similar to how
you might query for data in any other database.
Working with MongoDB and Spring Data similar to Spring Data with
JpaRepository and ReactiveCrudRepository.
How to install MongoDB
MongoDB is a document database and can be installed
locally or hosted in the cloud.
MongoDB Community Edition Server
https://www.mongodb.com/try/download/community
MongoDB Compass (GUI Tool)
https://www.mongodb.com/try/download/compass
Open MongoDB Compass
Create Database and Collection
College database
In mongodb database, the data will
be stored as JSON Documents under
some collection(Eg. students).
Object_id will be created for every
document automatically
Enabling Spring Data MongoDB
Spring Data MongoDB dependency enable us to access
data from MongoDB Database.
To enable Spring Data MongoDB Reactive dependency
as given be
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
Connecting MongoDB in Spring Boot application
spring.data.mongodb.uri
Single property enable us to connect MongoDB Database
spring.data.mongodb.uri=mongodb://localhost:27017/college
Mapping domain types to documents
Spring Data MongoDB offers a handful of annotations that
are useful for mapping domain types to document
structures to be persisted in MongoDB.
@Id—Designates a property as the document ID (from Spring Data
Commons)
@Document—Declares a domain type as a document to be persisted to
MongoDB
Step1: Create Domain Class
@Document(collection="students")
public class Students {
@Id
private ObjectId _id;
private String name;
private String email;
private String phone;
public ObjectId get_id() {
return _id;
}
public void set_id(ObjectId _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
Writing reactive MongoDB repository interfaces
Step2: Create Mongo Repository Interface
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends ReactiveMongoRepository<Students,ObjectId>
{
}
ReactiveMongoRepository provides implementation for the following database tasks
• Store new data
• Search an existing data
• Update existing data
• Delete existing data
• Display All data
Step3: Create Rest Controller Service
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("/getAll")
public Flux<Students> getAllUsers(){
return userRepository.findAll();
}
@GetMapping("/getOne/{id}")
public Mono<Students> getUserById(@PathVariable ObjectId id){
return userRepository.findById(id);
}
@PostMapping("/addUser")
public Mono<Students> addUser(@RequestBody Students students){
return userRepository.save(students);
}
}
Testing 1: View All Documents from mongodb
Testing 2: Search a document using Object_id from mongodb
Testing 3: Add a new document to mongodb database
New document
added for
“reban”