Top Spring WebFlux Interview Questions (2021) | CodeUsingJava
















Most frequently asked Spring WebFlux Interview Questions



  1. What is Spring Webflux?
  2. What are the benefits of Spring Webflux?
  3. What is spring reactive web?
  4. What is Reactive Programming in Spring Webflux?
  5. What are Reactive Streams API?
  6. What do you understand by the Spring IoC Container?
  7. What is bodyToMono?
  8. What is bodyToFlux?
  9. What does Mono.defer() do?
  10. What do you understand by Spring Beans?
  11. What is Spring MVC framework?
  12. How to handle an exception in a Spring Webflux based application?
  13. Can I use SpringMvc and webflux together?


What is Spring Webflux?

Spring Boot 2.0 was a long-awaited release from the good folks at Pivotal. One of its new features is reactive web programming support with Spring WebFlux. Spring WebFlux is a web framework that is built on top of Project Reactor, to give you asynchronous I/O, and allow your application to perform better.
Reactive web programming is great for applications that have streaming data, and clients that consume it and stream it to their users. It is not great for developing CRUD apps. If you want to develop a CRUD API, stick with Spring MVC and be happy about it.The new framework supports two programming models:
Annotation-based reactive components
Functional routing and handling

Webflux


What are the benefits of Spring Webflux?

  • Spring MVC is a web framework based on the Servlet API; such apps can be deployed on Servlet containers (like Jetty, Tomcat, Undertow).
  • Spring WebFlux is a reactive web framework based on a reactive HTTP layer; such apps can be deployed on Netty or Undertow (with native adapters) or Jetty/Tomcat/any Servlet 3.1 container (thanks to a Servlet 3.1 adapter).
  • Spring Boot applications can use Spring MVC or Spring WebFlux.

What is spring reactive web?

The spring-web-reactive module contains the Spring Web Reactive framework that supports the @Controller programming model. It re-defines many of the Spring MVC contracts such as HandlerMapping and HandlerAdapter to be asynchronous and non-blocking and to operate on the reactive HTTP request and response.

What is Reactive Programming in Spring Webflux?

Reactive programming is a programming paradigm that promotes an asynchronous, non-blocking, event-driven approach to data processing. Reactive programming involves modeling data and events as observable data streams and implementing data processing routines to react to the changes in those streams.
Before digging deeper into reactive world, first understand the difference between blocking vs non-blocking request processing.One important thing to remember is back pressure. In non-blocking code, it becomes important to control the rate of events so that a fast producer does not overwhelm its destination.
Reactive web programming is great for applications that have streaming data, and clients that consume it and stream it to their users. It is not great for developing traditional CRUD applications. If you are developing the next Facebook or Twitter with lots of data, a reactive API might be just what you are looking for.

What are Reactive Streams API?

Publisher: Emits a sequence of events to subscribers according to the demand received from its subscribers. A publisher can serve multiple subscribers. It has a single method:

Webflux
Subscriber: Receives and processes events emitted by a Publisher. Please note that no notifications will be received until Subscription#request(long) is called to signal the demand. It has four methods to handle various kind of responses received.

Webflux
Subscription: Defines a one-to-one relationship between a Publisher and a Subscriber. It can only be used once by a single Subscriber. It is used to both signal desire for data and cancel demand (and allow resource cleanup).

Webflux
Processor: Represents a processing stage consisting of both a Subscriber and a Publisher and obeys the contracts of both.

Webflux


What do you understand by the Spring IoC Container?

An IoC container is a common characteristic of frameworks that implement IoC. In the Spring framework, the interface ApplicationContext represents the IoC container. It is responsible for instantiating, configuring and assembling objects known as beans, as well as managing their life cycles.

What is bodyToMono?

One of the very common method used to deserialize the request and convert to Mono is bodyToMono
public Mono<Score resp.createException()
             .map(WebClientGraphqlException::new)
             .flatMap(Mono::error)
         ).bodyToMono(new ParameterizedTypeReference<Score<Student>>() {}); // This Line
  }
  

What is bodyToFlux?

This extension is not subject to type erasure and retains actual generic type arguments. Author.
public Mono<Score<Student>> getStudents(String id) {

    return webClient
         .get()
         .uri(uriBuilder -> uriBuilder
         .path("/students/{0}")
         .build(id))
         .retrieve()
         .onStatus(HttpStatus::isError, resp -> resp.createException()
             .map(WebClientGraphqlException::new)
             .flatMap(Mono::error)
         ).bodyToFlux(Student.class).collect(Collectors.toList()); // This Line
  }
  


What does Mono.defer() do?

Mono. defer(monoSupplier) lets you provide the whole expression that supplies the resulting Mono instance. The evaluation of this expression is deferred until somebody subscribes. Inside of this expression you can additionally use control structures like Mono.
When you run Mono.just() it create immediately an Observable(Mono)and reuse it but when you use defer it doesn't create it immediately it create new Observable in every subscribe .

What do you understand by Spring Beans?

Bean is a key concept of the Spring Framework. As such, understanding this notion is crucial to get the hang of the framework and to use it in an effective way.
In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.

What is Spring MVC framework?

The Spring Framework is an application framework and inversion of control container for the Java platform. The framework's core features can be used by any Java application, but there are extensions for building web applications on top of the Java EE platform.
The Model encapsulates the application data and in general they will consist of POJO.
The View is responsible for rendering the model data and in general it generates HTML output that the client's browser can interpret.
The Controller is responsible for processing user requests and building an appropriate model and passes it to the view for rendering.

framework


How to handle an exception in a Spring Webflux based application?

There are different ways to handle errors at functional level:
  • onErrorReturn
  • onErrorResume
  • onErrorMap
  • Handling Errors at a Global Level

Can I use SpringMvc and webflux together?

Spring MVC can't run on Netty
Both infrastructure will compete for the same job (for example, serving static resources, the mappings, etc)
mixing both runtime models within the same container is not a good idea and is likely to perform badly or just not work at all.
Depending on the goal you're trying to achieve, there might be several ways to work on this.
If you'd like to use WebClient to optimize for multiple, concurrent remote HTTP calls and use Reactor operators, you can keep using Spring MVC annotated controllers and return reactive types as return values (more on this in this Spring Boot talk). Finally, if you'd like to use the functional approach provided by Spring WebFlux, then it won't necessarily perform better. It really depends on your use case and how you implement it.