How to do it...

Let us utilize Reactive Spring Data JPA by performing the following steps:

  1. Convert ch12-mongodb to a Spring Boot 2.0 application by adding the Spring Boot 2.0.0.M2 starter POM dependencies, such as webflux, actuator for project status monitoring and management, Spring JDBC, MySQL connector, and the Spring Data JPA starter POM that we just used.
There is no reactive counterpart for the Spring Data JPA's CrudRepository and JpaRepository.
  1. Before anything else, verify that you have installed the recent binary for the MongoDB server in your system. If not, go back to Chapter 1, Getting Started with Spring, and follow the procedure on how to install the MongoDB server.
  2. To turn on the MongoDB server, open a command-line terminal and run the server using the inmongod command.
  3. Open another terminal and open the server shell through inmongo. Using MongoDB tutorials from https://docs.mongodb.com/manual/tutorial/, we are now ready to administer and manage a MongoDB database.
  4. Create a hrs database by running the use hrs command.
  5. Create an employee collection and insert a record in it using the following command:
db.employee.insert({ "id":23456, "firstName": "Sherwin John", "lastName": "Tragura", "age":39, "email":"[email protected]", "birthday":"10-30-1978", "deptid":359})
  1. Add more document records to employee and then view all Collections using the db.employee.find() command.
NoSQL databases, such as MongoDB, leverage JSON as the core data model. Thus, use data types that are recognizable only to JSON documents such as Long, Double, String, Boolean, and other JSON objects defined by MongoDB.
  1. After the entire MongoDB server configuration and database setup, let's create the Bootstrap class for our Spring Boot application inside a core package, org.packt.nosql.mongo.core:
@SpringBootApplication 
public class MongoBootApplication  
extends  SpringBootServletInitializer  { 
   // refer to sources 
} 
  1. Copy the config folder and logback.xml from the previous recipe. Update the log file details correctly to enable logging for this application.
  2. Create an application.properties file for this project containing all the server-related and MySQL database-related information for JPA autoconfiguration. The only irony here is that we will not be using any MySQL features in this recipe. We just need to comply with the JPA autoconfiguration process for the implementation of its transaction management beans.
  3. Inside org.packt.nosql.mongo.core.config, add a Reactive Spring Data configuration class that looks similar to the one from previous recipes:
@Configuration 
@EnableJpaRepositories( 
basePackages="org.packt.nosql.mongo.core.dao") 
@EnableTransactionManagement 
public class ReactiveDataConfig { } 
  1. Since the base storage unit of MongoDB is an entity called document, create an entity model needed for persisting the Employee documents into the Mongo database. Drop this document object inside the org.packt.nosql.mongo.core.model.data package:
@Document(collection="employee") 
public class Employee  { 
    
   private BigInteger _id; 
    
   @Id 
   private Long id; 
    
   private Long empid; 
   private String firstname; 
   private String lastname; 
   private Long age; 
   private String email; 
   private Date birthday; 
   private Long deptid; 
    
    
   @PersistenceConstructor 
   public Employee(Long id, BigInteger _id, Long empid,  
String firstname, String lastname, Long age,  
String email, Date birthday, Long deptid) { 
      super(); 
      this.id = id; 
      this._id = _id; 
      this.empid = empid; 
      this.firstname = firstname; 
      this.lastname = lastname; 
      this.age = age; 
      this.email = email; 
      this.birthday = birthday; 
      this.deptid = deptid;    
} 
// getters and setters 
} 
We declared the custom employee id as the primary @Id of the entity model and not BigInteger _id, which is the default object ID assigned to each document by the Mongo server. Likewise, there is a shift from Integer to Long data types in this entity model due to the data type constraints of JSON.
  1. Inside org.packt.nosql.mongo.core.dao, create a custom EmployeeRepository interface that will highlight the Reactive API class org.springframework.data.repository.reactive.ReactiveCrudRepository:
@Repository 
public interface EmployeeRepository  
extends ReactiveCrudRepository<Employee, Long>{ 
    
   public Flux<Employee> findAllById(Flux<Long> ids); 
   public Flux<Employee> findAllByFirstname(String fname); 
   public Flux<Employee> findAllByLastname(String lname); 
} 
  1. Create an Employee service class that will consist of the following template methods:
public interface EmployeeService { 
    
   public Flux<Employee> getAllEmps(); 
   public Flux<Employee> getAllEmps(Flux<Long> ids); 
   public Mono<Employee> getEmpByid(Long id); 
   public Flux<Employee> getEmpsByFname(String fname); 
   public Flux<Employee> getEmpsByLname(String lname); 
   public void saveEmp(Employee emp); 
   public void saveEmps(Flux<Employee> emps); 
} 
  1. Implement the preceding service class using the reactive EmployeeRepository class:
@Service 
public class EmployeeServiceImpl implements EmployeeService{ 
    
   @Autowired 
   private EmployeeRepository employeeRepository; 
 
   @Override 
   public Flux<Employee> getAllEmps() { 
      return employeeRepository.findAll(); 
   } 
 
   @Override 
   public Flux<Employee> getAllEmps(Flux<Long> ids) { 
      return employeeRepository.findAllById(ids); 
   } 
 
   @Override 
   public Mono<Employee> getEmpByid(Long id) { 
      return employeeRepository.findById(id); 
} 
// refer to sources 
} 
  1. Inside org.packt.nosql.mongo.core.controller, create EmReactiveController, which will execute the Reactive services implemented:
@RestController 
public class EmpReactiveController { 
    
   @Autowired 
   private EmployeeService employeeServiceImpl; 
    
   @GetMapping("/selectReactEmps") 
   public Flux<Employee> selectReactDepts() { 
      return employeeServiceImpl.getAllEmps(); 
   } 
    
   @GetMapping("/selectReactEmp/{id}") 
   public Mono<Employee> selectReactDept( 
@PathVariable("id") Long id) { 
      return employeeServiceImpl.getEmpByid(id); 
   } 
} 
  1. Save all files. Then clean, build, and deploy the application.
  2. Open a browser and run one of the REST services:
..................Content has been hidden....................

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