Tuesday, January 2, 2018

Spring Webflux - Part 2

Reactive Programming is one of the most popular programming paradigms now a days. Spring Webflux is the newest flavor of Spring Framework with the taste of Reactive Programming. In my first article I have discussed about the building boxes of Spring Webflux. So, in this article we will build a simple application with Spring Webflux.

Application Structure


We will create a Spring-boot Application as a Maven Project. Add the following dependencies to Maven-POM. 

<dependencies>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-webflux</artifactId>
 </dependency>
 <dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <optional>true</optional>
 </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>
</dependencies>

Annotate the Spring-Boot-Application class with @EnableWebFlux. Adding this annotation to an '@Configuration class' or 'Spring-Boot-Application class' imports the Spring Web Reactive configuration from WebFluxConfigurationSupport.

@SpringBootApplication
@EnableWebFlux
public class WebfluxSampleApplication {

 public static void main(String[] args) {
  SpringApplication.run(WebfluxSampleApplication.class, args);
 }
}

Building the Application


Spring-Boot is so simple as you can use Spring-Webflux as you use Spring-MVC. You can just use Spring-Controllers. Following is a sample how to use controllers with Spring-Webflux.

@RestController
public class IndexController {
    @GetMapping("/index")
    public Mono<String> getIndex() {
        return Mono.just("Hello World!");
    }
}

If you run the application and access 'http://localhost:8080/index' in your browser you will get a response of 'Hello World!'. And it is what you have to do to say 'Hello World!' in Reactive. But this is the most traditional way you do in Spring. But Reactive Programming is always comes with Functional Programming.

You can use Webflux in more functional way. You can write your own router functions and handlers for those routes. Routes are more likely the resource-paths. When you do a request to the resource path with the correct HTTP method the handler will handle the request and respond.

First lets write the handler function. A handler function will take a ServerRequest and returns Mono<ServerResponse>.

@Component
public class SampleHandler {
    public Mono<ServerResponse> handleRequest(ServerRequest request) {
        return ServerResponse.ok().body(Mono.just("Sample Route Successful!"), String.class);
    }
}

Now we can write the router for the handler. Whenever a request comes to the resource-path of the router it will calls the handler function and respond to the request.

@Configuration
public class SampleRouter {
    @Bean
    public RouterFunction<ServerResponse> sampleRoute(SampleHandler handler) {
        return route(GET("/simple-route"), handler::handleRequest);
    }
}

Now if you run the application and access 'http://localhost:8080/simple-route' from your browser, you will get 'Sample Route Successful!' as the response. So, that is how we can be more functional in Spring Webflux. In the next article we will discuss about more advance routing functions. The complete source code for this article can be found here.

No comments:

Post a Comment