How to do it...

Let us not use the dedicated Spring Data MongoDB to implement Reactive repository transactions:

  1. Add the following Reactive Spring Data MongoDB dependency in the pom.xml file:
<dependency>             
    <groupId>org.springframework.boot</groupId>           
    <artifactId>spring-boot-starter-data-mongodb-reactive 
</artifactId>       
</dependency> 
  1. Open the application.properties file and add the following MongoDB server configuration details:
spring.data.mongodb.host=localhost 
spring.data.mongodb.port=27017 
spring.data.mongodb.database=app1 
  1. Before we proceed, create another Collections in the hrs database, but this time composed of the Department records. Insert a few documents using the Mongo commands mentioned in the previous recipe.
  2. Go back again to the Spring Eclipse STS. Create a Department model, which will be used by the application to transact with the Mongo database. Drop this file inside the org.packt.nosql.mongo.core.model.data package:
@Document(collection="department") 
public class Department { 
    
   private BigInteger _id; 
    
   @Id 
   private Long id; 
   private Long deptid; 
   private String name; 
    
   @PersistenceConstructor 
   public Department(BigInteger _id, Long id, Long deptid,  
String name) { 
      super(); 
      this._id = _id; 
      this.id = id; 
      this.deptid = deptid; 
      this.name = name; 
} 
// setters and getters 
}
  1. Create a configuration class inside org.packt.nosql.mongo.core.config, which will enable MongoDB transaction management. This class must inherit org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration to inject some beans needed to establish Reactive MongoDB CRUD transactions, such as ReactiveMongoTemplate:
@Configuration     
@EnableReactiveMongoRepositories( 
basePackages="org.packt.nosql.mongo.core.dao") 
@EnableWebFlux 
public class MongoConfig  
extends AbstractReactiveMongoConfiguration { 
 
   @Override 
   public MongoClient mongoClient() { 
      return MongoClients.create(); 
   } 
 
   @Override 
   protected String getDatabaseName() { 
      return "hrs"; 
   } 
    
   @Bean      
   public ReactiveMongoTemplate reactiveMongoTemplate() {           
      return new ReactiveMongoTemplate( 
mongoClient(), getDatabaseName());       
   } 
} 
  1. Now, let's create a MongoDB Reactive repository class inside org.packt.nosql.mongo.core.dao using its own Spring Data MongoDB module:
@Repository 
public interface DepartmentRepository  
extends ReactiveMongoRepository<Department, Long>{ 
   public Flux<Department> findAllById(Flux<Long> ids); 
   public Flux<Department> findAllByName(String name); 
} 
  1. Create a Department service class that consists of all Reactive services:
public interface DepartmentService { 
   public Flux<Department> getAllDepts(); 
   public Flux<Department> getAllDepts(Flux<Long> ids); 
   public Mono<Department> getDeptByid(Long id); 
   public void saveDept(Department dept); 
   public void saveDepts(Flux<Department> depts); 
} 
  1. Implement all the services in DepartmentService using DepartmentRepository:
@Service 
public class DepartmentServiceImpl implements  
   DepartmentService { 
    
   @Autowired 
   private DepartmentRepository departmentRepository; 
 
   @Override 
   public Flux<Department> getAllDepts() { 
      return departmentRepository.findAll(); 
   } 
 
   @Override 
   public Flux<Department> getAllDepts(Flux<Long> ids) { 
      return departmentRepository.findAllById(ids); 
   } 
 
   @Override 
   public Mono<Department> getDeptByid(Long id) { 
      return departmentRepository.findById(id); 
   } 
   // refer to sources 
} 
  1. Create REST services to use the following Reactive services. Save all files. Then clean, build, and deploy the application.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.166.255