DEV Community

loizenai
loizenai

Posted on

SpringBoot WebFlux Functional RestAPIs

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:

I. Technologies

– Java: 1.8
– Maven: 3.3.9
– Spring Tool Suite: Version 3.9.0.RELEASE
– Spring Boot: 2.0.0.M4
– Spring Boot Starter Webflux

II. Spring WebFlux Functional

Spring Framework 5.0 supports WebFlux with fully asynchronous and non-blocking and does NOT require the Servlet API(Unlike Spring MVC).

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.

1. HandlerFunctions

HandlerFunctions will handle incoming HTTP requests through 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)));
    }
}

2. RouterFunction

RouterFunction handle all incoming requests. It takes a 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

Billboard image

Imagine monitoring that's actually built for developers

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

Top comments (0)

Imagine monitoring actually built for developers

Billboard image

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay