Spring WebFlux

In this section, we will go into a bit more detail on Spring WebFlux. There are two (programming model) ways by which Spring WebFlux can be used. They are as follows:

  • Using annotations: By using annotations such as @Controller similar to how it is been done in Spring MVC
  • Using functional style: By using routing and handling with Java Lambdas

The following code shows the annotation-based style of using Spring WebFlux. We will be going through the entire code sample in subsequent sections in this chapter. This section, however, is aimed at giving an introduction before we delve deeper:

@RestController
@RequestMapping(value=”/api/movie”)
public class MovieAPI {
@GetMapping(“/”)
public Flux(Movie) getMovies() {
//Logic of getting all movies
}
@GetMapping(“/{id}”)
public Mono<Movie> getMovie(@PathVariable Long id) {
//Logic for getting a specific movie
}
@PostMapping(“/post”)
public Mono<ResponseEntity<String>> createMovie(@RequestBody Movie movie) {
// Logic for creating movie
}
}

The functional-style programming model of Spring WebFlux uses two fundamental components:

  • HandlerFunction: Entrusted to handle an HTTP request. Equivalent to @Controller handler methods we have seen in our previous code snippet.
  • RouterFunction: Entrusted to route an HTTP request. Equivalent to @RequestMapping in annotation-based.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.100.243