TOP Spring WebFlux Interview Questions and Answers (2024) | TechGeekNext


TOP Spring WebFlux Interview Questions and Answers (2024)

In this post, questions from Spring WebFlux Interviews will be answered for Experienced and Freshers. We're trying to share our experience and learn how to help you make progress in your career.

Q: What is Spring Webflux?
Ans:

Spring WebFlux is part of Spring 5, it is parallel version of Spring MVC and supports fully non-blocking reactive streams. It support the back pressure concept and uses Netty as inbuilt server to run reactive applications.

Spring Webflux

You can easily work on webflux if you are familiar with the Spring MVC programming style.

Q: What is spring reactive web?
Ans:

The spring-web-reactive module contains the Spring Web Reactive framework that supports the @Controller programming model. Many Spring MVC contracts, such as HandlerMapping and HandlerAdapter, are re-defined to be asynchronous and non-blocking, and to work with reactive HTTP requests and responses.

Checkout our related posts :

Q: Why is spring reactive?
Ans:

Reactive programming enables you to build systems which is resilient to high load. Handling lots of traffic isn't a problem because the server is non-blocking and doesn't block client processes to wait for responses.

Q: Is Spring WebClient thread safe?
Ans:

Is Spring WebClient thread safe? WebClient is thread-safe since it is immutable. WebClient is designed to work in a reactive environment where nothing is tied to a single thread (though you can still use it in a typical Servlet application).

Q: What is bodyToMono?
Ans:

It is used to deserialize the request and convert to Mono is bodyToMono i.e. "Extract the body to a Mono". Use bodyToMono for retrieving a single item, it emits 0-1 items.

Mono<Employee> employeeMonoObj = client.get()
  .uri("/employees/{id}", "1")
  .retrieve()
  .bodyToMono(Employee.class);

employeeMonoObj.subscribe(System.out::println);

Refer WebFlux Mono Example.

Q: Is RestTemplate deprecated?
Ans:

RestTemplate allows you to consume Rest resources in a synchronous manner, which means it will block the thread before it receives a response. RestTemplate has been deprecated since Spring 5, so it's not quite future-proof.

Q: What is bodyToFlux?
Ans:

Similar to bodyToMono, however here it is used to to retrieve a collection resource of type Flux from endpoint /employees.

Flux<Employee> employeeFluxObj = client.get()
  .uri("/employees")
  .retrieve()
  .bodyToFlux(Employee.class);

employeeFluxObj.subscribe(System.out::println);

Refer WebFlux bodyToFlux Example.

Q: What is the difference between bodyToMono and bodyToFlux?
Ans:

  1. bodyToMono
    Used to retrieve a single item. It emits 0-1 items
  2. bodyToFlux
    Used to retrieve multiple items. It emits 0-N items.

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

There are different ways to handle errors at functional level:

  1. onErrorReturn
    Whenever an error occurs, use onErrorReturn() to return a static default value.
  2. onErrorResume
    In case you encounter an error, call fallback method.
  3. onErrorMap
    To provide custom exceptions.
  4. Handling Errors at a Global Level
    To handle WebFlux errors at a global level, extend the DefaultErrorAttributes class to customize the Global Error Response Attributes and then Implement the Global Error Handler.








Recommendation for Top Popular Post :