https://grokonez.com/spring-framework/spring-webflux/springboot-webflux-functional-restapis
SpringBoot WebFlux Functional RestAPIs
Reactive programming is about non-blocking applications. And Spring Framework 5 includes a new spring-webflux module
, supports Reactive Streams for communicating backpressure across async components and libraries. So in the tutorial, JavaSampleApproach will guide you through the steps for creating a SpringBoot WebFlux Functional restful APIs.
Related posts:
Spring WebFlux supports 2 distinct programming models:
– Annotation-based with @Controller
– Functional with Java 8 lambda style
In the tutorial, we will introduce WebFlux with Functional.
For starting with WebFlux, SpringBoot supports a collection dependency: spring-boot-starter-webflux
.
With Spring WebFlux Functional, we use {HandlerFunctions
, RouterFunctions
} to develop.
ServerRequest
, and return a Mono
@Component
public class CustomerHandler {
...
public Mono getAll(ServerRequest request) {
...
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(customers, Customer.class);
}
...
public Mono putCustomer(ServerRequest request) {
...
return responseMono
.flatMap(cust -> ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(fromObject(cust)));
}
...
public Mono deleteCustomer(ServerRequest request) {
...
return responseMono
.flatMap(strMono -> ServerResponse.ok().contentType(MediaType.TEXT_PLAIN).body(fromObject(strMono)));
}
}
ServerRequest
, and returns a Mono
. If a request matches a particular route, a handler function is returned; otherwise it returns an empty Mono.
More at:
https://grokonez.com/spring-framework/spring-webflux/springboot-webflux-functional-restapis
SpringBoot WebFlux Functional RestAPIs
#springboot #webflux #restapi #functional