12 Developing reactive APIs

 

This chapter covers

  • Using Spring WebFlux
  • Writing and testing reactive controllers and clients
  • Consuming REST APIs
  • Securing reactive web applications

Now that you’ve had a good introduction to reactive programming and Project Reactor, you’re ready to start applying those techniques in your Spring applications. In this chapter, we’re going to revisit some of the controllers you wrote in chapter 7 to take advantage of Spring’s reactive programming model.

More specifically, we’re going to take a look at Spring’s reactive web framework—Spring WebFlux. As you’ll quickly discover, Spring WebFlux is remarkably similar to Spring MVC, making it easy to apply, along with what you already know about building REST APIs in Spring.

12.1 Working with Spring WebFlux

Typical servlet web frameworks, such as Spring MVC, are blocking and multithreaded in nature, using a single thread per connection. As requests are handled, a worker thread is pulled from a thread pool to process the request. Meanwhile, the request thread is blocked until it’s notified by the worker thread that it’s finished.

12.1.1 Introducing Spring WebFlux

12.1.2 Writing reactive controllers

12.2 Defining functional request handlers

12.3 Testing reactive controllers

12.3.1 Testing GET requests

12.3.2 Testing POST requests

12.3.3 Testing with a live server

12.4 Consuming REST APIs reactively

12.4.1 GETting resources

12.4.2 Sending resources

12.4.3 Deleting resources

12.4.4 Handling errors

12.4.5 Exchanging requests