Signed-off-by: Pavitra Mallemdoddi Papili <ppapili@nisum.com>

parent c6ee8330
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.poc.web</groupId>
<artifactId>webflux-mongo-poc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>webflux-mongo-poc</name>
<description>Demo project for Webflux with spring boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>tomcat-embed-el</artifactId>
<groupId>org.apache.tomcat.embed</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.poc.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebfluxMongoPocApplication {
public static void main(String[] args) {
SpringApplication.run(WebfluxMongoPocApplication.class, args);
}
}
package com.poc.web.controller;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.poc.web.entity.Employee;
import com.poc.web.model.Emp;
import com.poc.web.service.EmployeeService;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@RestController
@RequestMapping(path = "/employees")
@Validated
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping(path = "/")
public ResponseEntity<Flux<Emp>> findAll() {
return new ResponseEntity<>(employeeService.findAll(), HttpStatus.OK);
}
@PostMapping(produces = "application/json", consumes = "application/json", path = "/save")
public ResponseEntity<Mono<Void>> saveEmployee(@Valid @RequestBody Emp emp) {
return new ResponseEntity<>(employeeService.save(emp), HttpStatus.OK);
}
@GetMapping(produces = "application/json", consumes = "application/json", path = "/empId/{empId}")
public ResponseEntity<Mono<Emp>> findByEmpId(@Valid @PathVariable Long empId) {
return new ResponseEntity<>(employeeService.findByEmpId(empId), HttpStatus.OK);
}
@GetMapping(produces = "application/json", consumes = "application/json", path = "/id/{id}")
public ResponseEntity<Mono<Emp>> findById(@Valid @PathVariable String id) {
return new ResponseEntity<>(employeeService.findById(id), HttpStatus.OK);
}
@GetMapping(produces = "application/json", consumes = "application/json", path = "/sal/{sal}")
public ResponseEntity<Flux<Emp>> findBySal(@Valid @PathVariable Double sal) {
return new ResponseEntity<>(employeeService.findBySalary(sal), HttpStatus.OK);
}
@DeleteMapping(produces = "application/json", consumes = "application/json", path = "/delete/{empId}")
public ResponseEntity<Mono<Long>> deleteByEmpId(@PathVariable Long empId) {
return new ResponseEntity<>(employeeService.deleteByEmpId(empId), HttpStatus.OK);
}
}
package com.poc.web.entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
@Document(collection = "employee")
public class Employee {
@Id
private String _id;
@Field(name = "emp_id")
@Indexed(unique=true)
private Long empId;
@Field(name = "emp_name")
private String empName;
@Field(name = "emp_sal")
private Double empSal;
@Field(name = "dept_id")
private String deptId;
@Field(name = "dept_name")
private String deptName;
public Employee() {
super();
}
/**
* @return the _id
*/
public String get_id() {
return _id;
}
/**
* @param _id the _id to set
*/
public void set_id(String _id) {
this._id = _id;
}
/**
* @return the empId
*/
public Long getEmpId() {
return empId;
}
/**
* @param empId the empId to set
*/
public void setEmpId(Long empId) {
this.empId = empId;
}
/**
* @return the empName
*/
public String getEmpName() {
return empName;
}
/**
* @param empName the empName to set
*/
public void setEmpName(String empName) {
this.empName = empName;
}
/**
* @return the empSal
*/
public Double getEmpSal() {
return empSal;
}
/**
* @param empSal the empSal to set
*/
public void setEmpSal(Double empSal) {
this.empSal = empSal;
}
/**
* @return the deptId
*/
public String getDeptId() {
return deptId;
}
/**
* @param deptId the deptId to set
*/
public void setDeptId(String deptId) {
this.deptId = deptId;
}
/**
* @return the deptName
*/
public String getDeptName() {
return deptName;
}
/**
* @param deptName the deptName to set
*/
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}
package com.poc.web.mapper;
import java.util.Optional;
import org.springframework.stereotype.Component;
import com.poc.web.entity.Employee;
import com.poc.web.model.Emp;
import reactor.core.publisher.Mono;
@Component
public class EmployeeMapper implements EntityMapper {
@Override
public Object mapPojoToEntity(Object pojo) {
Optional<Object> obj=Optional.ofNullable(pojo);
if(obj.isPresent()) {
Emp emp=(Emp)pojo;
Employee employee=new Employee();
employee.setDeptId(emp.getDeptId());
employee.setDeptName(emp.getDeptName());
employee.setEmpId(emp.getEmpId());
employee.setEmpName(emp.getEmpName());
employee.setEmpSal(emp.getEmpSal());
return employee;
}
return null;
}
@Override
public Object mapEntityToPojo(Object entity) {
if(entity instanceof Mono) {
System.out.println("Mono");
}else if(entity instanceof Employee) {
System.out.println("Employee");
}
Optional<Object> obj=Optional.ofNullable(entity);
if(obj.isPresent()) {
Employee employee=(Employee)entity;
Emp emp=new Emp();
emp.setDeptId(employee.getDeptId());
emp.setDeptName(employee.getDeptName());
emp.setEmpId(employee.getEmpId());
emp.setEmpName(employee.getEmpName());
emp.setEmpSal(employee.getEmpSal());
return emp;
}
return null;
}
}
package com.poc.web.mapper;
public interface EntityMapper {
public Object mapPojoToEntity(Object pojo);
public Object mapEntityToPojo(Object entity);
}
package com.poc.web.model;
import javax.validation.constraints.NotNull;
public class Emp {
@NotNull(message = "empID should not be null")
private Long empId;
@NotNull(message = "empName should not be null")
private String empName;
@NotNull(message = "empSal should not be null")
private Double empSal;
@NotNull(message = "deptId should not be null")
private String deptId;
@NotNull(message = "deptName should not be null")
private String deptName;
public Emp() {
super();
}
public Emp(@NotNull(message = "empID should not be null") Long empId,
@NotNull(message = "empName should not be null") String empName,
@NotNull(message = "empSal should not be null") Double empSal,
@NotNull(message = "deptId should not be null") String deptId,
@NotNull(message = "deptName should not be null") String deptName) {
super();
this.empId = empId;
this.empName = empName;
this.empSal = empSal;
this.deptId = deptId;
this.deptName = deptName;
}
/**
* @return the empId
*/
public Long getEmpId() {
return empId;
}
/**
* @param empId the empId to set
*/
public void setEmpId(Long empId) {
this.empId = empId;
}
/**
* @return the empName
*/
public String getEmpName() {
return empName;
}
/**
* @param empName the empName to set
*/
public void setEmpName(String empName) {
this.empName = empName;
}
/**
* @return the empSal
*/
public Double getEmpSal() {
return empSal;
}
/**
* @param empSal the empSal to set
*/
public void setEmpSal(Double empSal) {
this.empSal = empSal;
}
/**
* @return the deptId
*/
public String getDeptId() {
return deptId;
}
/**
* @param deptId the deptId to set
*/
public void setDeptId(String deptId) {
this.deptId = deptId;
}
/**
* @return the deptName
*/
public String getDeptName() {
return deptName;
}
/**
* @param deptName the deptName to set
*/
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}
package com.poc.web.repository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;
import com.poc.web.entity.Employee;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Repository
public interface EmpMongoRepository extends ReactiveMongoRepository<Employee, String> {
@Query(value="{emp_id : ?0}",sort = "{emp_id : 1}")
public Mono<Employee> findByEmpId(Long empId);
/**
* -1 -decending
* 1 ascending
* @param salary
* @return
*/
@Query(value="{emp_sal : {$gte: ?0}}",sort = "{emp_sal : -1}")
public Flux<Employee> findBySalary(Double salary);
@Query(value = "{emp_id : ?0}", delete = true)
public Mono<Long> deleteByEmployeeId(Long empId);
}
package com.poc.web.service;
import com.poc.web.entity.Employee;
import com.poc.web.model.Emp;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
public interface EmployeeService {
public Mono<Emp> findByEmpId(Long empId);
public Mono<Emp> findById(String id);
public Flux<Emp> findAll();
public Mono<Void> save(Emp emp);
public Flux<Emp> findBySalary(Double sal);
public Mono<Long> deleteByEmpId(Long empId);
}
package com.poc.web.service;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.poc.web.entity.Employee;
import com.poc.web.mapper.EmployeeMapper;
import com.poc.web.model.Emp;
import com.poc.web.repository.EmpMongoRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmpMongoRepository empMongoRepository;
@Autowired
private EmployeeMapper employeeMapper;
@Override
public Mono<Emp> findByEmpId(Long empId) {
return Mono.justOrEmpty((Emp) employeeMapper.mapEntityToPojo(empMongoRepository.findByEmpId(empId)));
}
@Override
public Mono<Emp> findById(String id) {
return Mono.just(
(Emp) employeeMapper.mapEntityToPojo(empMongoRepository.findById(id).defaultIfEmpty(new Employee())));
}
@Override
public Flux<Emp> findAll() {
Flux<Employee> list=empMongoRepository.findAll();
Flux<Emp> empList = list.map(e ->employeeMapper.mapEntityToPojo(e)).map(e -> (Emp) e);
return empList;
}
@Override
public Mono<Void> save(Emp emp) {
Employee employee = (Employee) employeeMapper.mapPojoToEntity(emp);
return empMongoRepository.save(employee).then();
}
@Override
public Flux<Emp> findBySalary(Double sal) {
Flux<Employee> stream = empMongoRepository.findBySalary(sal);
Flux<Emp> empList = stream.map(e -> employeeMapper.mapEntityToPojo(e)).map(e -> (Emp) e);
return empList;
}
@Override
public Mono<Long> deleteByEmpId(Long empId) {
return empMongoRepository.deleteByEmployeeId(empId);
}
}
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=ownDatabase
spring.data.mongodb.auto-index-creation=true
logging.level.org.springframework.data.mongodb.core.ReactiveMongoTemplate=DEBUG
logging.level.org.springframework.data.mongodb.repository.support.SimpleReactiveMongoRepository=DEBUG
logging.level.org.springframework.data.mongodb.repository.ReactiveMongoRepository=DEBUG
logging.level.org.springframework.boot.web.embedded.netty.NettyWebServer=DEBUG
logging.level.org.springframework=INFO
logging.level.root=INFO
server.port=8084
spring.webflux.base-path=/webflux-mongo-poc
package com.poc.web;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class WebfluxMongoPocApplicationTests {
@Test
void contextLoads() {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment