Spring 5 : WebFlux Example With Spring Boot| Code Factory

What is Spring WebFlux ?

Mono<String> mono = Mono.just("CodeFactory");
Mono<String> mono = Mono.empty();
Flux<String> flux = Flux.just("A", "B", "C");
Flux<String> flux = Flux.fromArray(new String[]{"A", "B", "C"});
Flux<String> flux = Flux.fromIterable(Arrays.asList("A", "B", "C"));
<?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.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.spring5crud</groupId>
<artifactId>spring5-crud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring5-crud</name>
<description>Demo project for Spring 5 with Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
</pattern>
</encoder>
</appender>

<logger name="org.springframework" level="DEBUG"
additivity="false">
<appender-ref ref="STDOUT" />
</logger>

<root level="ERROR">
<appender-ref ref="STDOUT" />
</root>

</configuration>
package com.spring5crud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author code.factory
*
*/
@SpringBootApplication
public class Spring5CrudApplication {
public static void main(String[] args) {
SpringApplication.run(Spring5CrudApplication.class, args);
}
}
package com.spring5crud.configuration;import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
/**
* @author code.factory
*
*/
@Configuration
public class AppConfig {
@Bean
public static PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocation(new ClassPathResource("application.properties"));
ppc.setIgnoreUnresolvablePlaceholders(true);
return ppc;
}
}
package com.spring5crud.configuration;import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.config.WebFluxConfigurer;
/**
* @author code.factory
*
*/
@Configuration
@EnableWebFlux
public class WebFluxConfig implements WebFluxConfigurer {
}
package com.spring5crud.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.spring5crud.model.Employee;
import com.spring5crud.service.EmployeeService;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* @author code.factory
* add Content-Type = application/json on every request
*/
@RestController
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<Flux<Employee>> findAll() {
Flux<Employee> emps = employeeService.findAll();
HttpStatus status = emps != null ? HttpStatus.OK : HttpStatus.NOT_FOUND;
return new ResponseEntity<Flux<Employee>>(emps, status);
}

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<Mono<Employee>> findById(@PathVariable("id") Integer id) {
Mono<Employee> e = employeeService.findById(id);
HttpStatus status = e.equals(Mono.empty()) ? HttpStatus.NOT_FOUND : HttpStatus.OK;
return new ResponseEntity<Mono<Employee>>(e, status);
}

@RequestMapping(value = { "/create" }, method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public void create(@RequestBody Employee e) {
employeeService.create(e);
}
@RequestMapping(value = "/update", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<Mono<Employee>> update(@RequestBody Employee emp) {
Mono<Employee> e = employeeService.findById(emp.getId());
HttpStatus status = e.equals(Mono.empty()) ? HttpStatus.NOT_FOUND : HttpStatus.OK;
if(e.equals(Mono.empty())) {
return new ResponseEntity<Mono<Employee>>(Mono.empty(), status);
}
return new ResponseEntity<Mono<Employee>>(employeeService.update(emp), status);
}

@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<Mono<Void>> delete(@PathVariable("id") Integer id) {
Mono<Employee> e = employeeService.findById(id);
HttpStatus status = e.equals(Mono.empty()) ? HttpStatus.NOT_FOUND : HttpStatus.OK;
if(e.equals(Mono.empty())) {
return new ResponseEntity<Mono<Void>>(Mono.empty(), status);
}
return new ResponseEntity<Mono<Void>>(employeeService.delete(id), status);
}
}
package com.spring5crud.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* @author code.factory
*
*/
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
int id;
String name;
long salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
@Override
public String toString() {
return "id=" + id + ", name=" + name + ", salary=" + salary;
}
}
package com.spring5crud.Repository;import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.spring5crud.model.Employee;/**
* @author code.factory
*
*/
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
}
package com.spring5crud.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spring5crud.Repository.EmployeeRepository;
import com.spring5crud.model.Employee;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* @author code.factory
*
*/
@Service
public class EmployeeService implements IEmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
@Override
public void create(Employee e) {
employeeRepository.save(e);
}
@Override
public Mono<Employee> findById(Integer id) {
return Mono.justOrEmpty(employeeRepository.findById(id));
}
@Override
public Flux<Employee> findAll() {
return Flux.fromIterable(employeeRepository.findAll());
}
@Override
public Mono<Employee> update(Employee e) {
return Mono.just(employeeRepository.save(e));
}
@Override
public Mono<Void> delete(Integer id) {
employeeRepository.deleteById(id);
return Mono.empty();
}
}
package com.spring5crud.service;import com.spring5crud.model.Employee;import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* @author code.factory
*
*/
public interface IEmployeeService {
void create(Employee e);Mono<Employee> findById(Integer id);Flux<Employee> findAll();Mono<Employee> update(Employee e);Mono<Void> delete(Integer id);}
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@ResponseBody
public ResponseEntity<Flux<Employee>> findAll() {
Flux<Employee> emps = employeeService.findAll();
HttpStatus status = emps != null ? HttpStatus.OK : HttpStatus.NOT_FOUND;
return new ResponseEntity<Flux<Employee>>(emps, status);
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store