Spring boot spring data MongoDB reactive crud example tutorial that explains how to create a REST api using Spring boot 2, Spring data MongoDB reactive, Spring Webflux and MongoDB database.
This tutorial is a modification of the tutorial Spring Boot Spring Data MongoDB database tutorial.
Adding Spring data MongoDB reactive and Spring webflux maven dependencies
Spring Webflux replaces Spring MVC and implements the Reactor framework. It has the same annotations as Spring MVC and it doesn’t dépend on the Servlet API.
The dependencies to be added to the maven POM file are the followings :
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId> </dependency>
Setting up Spring Data Mongo Reactive Repository using JAVA configuration class
The configuration class of the Spring Data MongoDB reactive Repository is the following:
package com.javasneo.todolist.config; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories; @Configuration @EnableReactiveMongoRepositories(basePackages = "com.javasneo.todolist.repository") public class ReactiveMongoConfig { }
Here, we activate Spring Data Mongo Reactive Repositories by
providing the package where those repositories can be found.
Updating the REST API to use MongoDB database reactive
In order to update the Spring Boot REST API example, we will :
- Update repository that makes the CRUD operations
- Update service that uses this repository
- Update the controller to use this service
In order to be full reactive, the repositories, the services
and the controllers should return a reactive type (Flux, Mono) and take a reactive type as possible.
To get a good introduction about Spring webflux and the
reactive programming using Spring Framework 5, you may check the part 3 of this
book.
Rest API Repository
The repository is a java interface with no javaclass implementing it. This is a spring Data Mongodb reactive
feature that auto-generates the CRUD code for us based on an interface that we
specify.
The code of the repository is the following :
package com.javasneo.todolist.repository; import com.javasneo.todolist.model.Todo; import org.springframework.data.mongodb.repository.ReactiveMongoRepository; public interface TodoRepository extends ReactiveMongoRepository<Todo, String> { }
The TodoRepository interface should extend the Spring Data MongoDB
reactive interface ReactiveMongoRepository and give two parameters to that
interface :
- The first one is the Type of the Entity that we will generate the CRUD code for.
- The second one is the type of the primary key of the entity (the id attribute in the Todo class is of type String)
Rest API Service
The service layer of the application is composed by the
interface of the service and its implementation. Here, we changed the return
type of the methods to be either Mono or Flux.
package com.javasneo.todolist.service; import com.javasneo.todolist.model.Todo; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; public interface TodoService { Flux<Todo> getAll(); Mono<Todo> saveOrUpdate(Todo todo); void delete(String id); }
The source code of the implementation is the following :
package com.javasneo.todolist.service; import com.javasneo.todolist.model.Todo; import com.javasneo.todolist.repository.TodoRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @Service public class TodoServiceImpl implements TodoService{ @Autowired TodoRepository todoRepository; public Flux<Todo> getAll(){ return todoRepository.findAll(); } public Mono<Todo> saveOrUpdate(Todo todo) { return todoRepository.save(todo); } public void delete(String id) { todoRepository.deleteById(id); } }
Rest API controller
The REST controller uses the service layer to provide the
CRUD endpoints.
The source code of the controller is the following :
package com.javasneo.todolist.controller; import com.javasneo.todolist.model.Todo; import com.javasneo.todolist.service.TodoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @RestController @RequestMapping("/todos") @CrossOrigin() public class TodoController { @Autowired TodoService todoService; @RequestMapping(method = RequestMethod.GET) public Flux<Todo> todos (){ return todoService.getAll(); } @RequestMapping(method = RequestMethod.POST) public Mono<Todo> addtodo (@RequestBody Todo todo){ return todoService.saveOrUpdate(todo); } @RequestMapping(method = RequestMethod.PUT) public Mono<Todo> updateTodo (@RequestBody Todo todo){ return todoService.saveOrUpdate(todo); } @RequestMapping(value="/{id}", method = RequestMethod.DELETE) public void deleteTodo (@PathVariable("id") String id){ todoService.delete(id); } }
Conclusion
This spring boot spring data MongoDB reactive crud example tutorial arrives
at its end.
If you want to know how to test the endpoints using Postman, please check
the part 1 of these series of tutorials that deal with Spring Boot 2 REST API.
You may check also our core java tutorial to learn core java from
scratch. Please keep tuned because we will release more tutorials about Spring
Boot, unit testing, integration testing and other important subjects.
In order to get our latest updates, please feel free to like our how to program facebook page.
Post a Comment