The Wayback Machine - https://web.archive.org/web/20240718085020/https://www.geeksforgeeks.org/spring-webflux-hello-world-example/
Open In App

Spring WebFlux Hello World Example

Last Updated : 28 Feb, 2024
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

The Spring Web Flux provides different applications to developers to develop asynchronous, non-blocking web applications by using Mono, Flux, and other classes. In this article, we will be learning to write a basic Hello World program in Spring WenFlux.

Project Creation:

  • Open STS (Spring Tool Suite) and then select New Project.
  • After that, we will be redirected to a project creation screen, in Select Project Category like maven or gradle, choose any (Here we will be using Gradle).
  • Then provide the project name, package name, and other details.
  • Then select dependencies.
  • Now click on finish.

Project Folder Structure:

Project Structure

Project Dependencies:

In this project, we have used below dependencies and the project category is gradle.

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
}

Hello World Program in Spring WebFlux

Here, we have provided a basic example for printing Hello World by using @RestController.

  • For this, in main package, we have created one Java class named as HelloWorldController.
  • After that, we have created one @GetMapping end point with name hello.
  • The @GetMapping is used for creating API End points.
  • While open with browser, this return output as Hello, World!.

HelloWorldController class:

Java




package com.app;
  
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
  
@RestController
public class HelloWorldController {
  
    @GetMapping("/hello")
    public Mono<String> hello() {
        return Mono.just("Hello, World!");    //prints Hello, World!
    }
}


Main Class:

This is a main class of the project.

Java




package com.app;
  
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
  
@SpringBootApplication
public class HelloWorldApplication {
    // Main Method
    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }
}


After successfully running this project as Spring Boot App, open any browser then type below API End Point URL:

http://localhost:8080/hello

Output:

Below we can see the output in browser.

Output in browser

Explanation of the above Program:

  • First, we have created one class in main package of project.
  • The class name is HelloWorldController.
  • After this, we have created one Get Mapping by using @GetMapping with parameter /hello.
  • Next, we run this project as Spring Boot Application.
  • After running, open browser then type the Get mapping URL that is provided in the above.
  • Once hit that API end point, we got the expected output that is visible in the output screen.


Similar Reads

Spring - Hello World Example
In this tutorial, we will learn step by step how to develop a Spring Hello World application in STS (Spring Tool Suite), and also we will get to know how we can add dependency and how to do bean configuration into our application. Note: We can also use Eclipse IDE or IntelliJ IDE for developing Spring Applications. But here we will be using STS for
5 min read
Difference Between Spring MVC and Spring WebFlux
Spring MVCSpring MVC Framework takes on the Model-View-Controller design pattern, which moves around the Dispatcher Servlet, also called the Front Controller. With the help of annotations like @Controller and @RequestMapping, the by-default handler becomes a robust(strong) tool with a diverse set of handling ways. This kind of dynamic kind of featu
5 min read
Spring Boot - Reactive Programming Using Spring Webflux Framework
In this article, we will explore React programming in Spring Boot, Reactive programming is an asynchronous, non-blocking programming paradigm for developing highly responsive applications that react to external stimuli. What is Reactive ProgrammingIn reactive programming, the flow of data is asynchronous through push-based publishers and subscriber
4 min read
Spring WebFlux REST Application Integration with Spring Data R2DBC
Spring WebFlux is the framework from the Spring ecosystem that supports reactive programming for building asynchronous and non-blocking web applications. Unlike the traditional Spring MVC, which can use the blocking I/O. WebFlux can be designed to handle large volumes of requests using fewer resources by leveraging reactive streams. The main idea o
4 min read
Security with Spring Security and Spring Webflux
Spring WebFlux is a part of the Spring Framework that supports reactive programming, enabling non-blocking asynchronous request handling. When developing web applications with Spring WebFlux, securing the application is a crucial aspect to ensure unauthorized access is prevented. This article provides a comprehensive example of integrating Spring S
3 min read
Spring Cloud Gateway with Spring WebFlux
Spring Cloud Gateway is the library that allows you to build API gateways on top of Spring WebFlux. It provides a simple and effective way to route requests, apply filters, and manage cross-cutting concerns reactively. This article will guide you through demonstrating how to set up Spring Cloud Gateway with a simple microservice and route requests
5 min read
Spring WebFlux Functional Endpoints CRUD REST API Example
Spring Boot is a Java framework for back-end development. Another miracle in Spring Boot is the Spring Boot Community has developed the Spring Reactive Web Framework i.e. Spring WebFlux. To develop this project, we have used the MongoDB database. Here, for every CRUD (Create, Retrieve, Update, Delete) operation, we have created one REST API endpoin
7 min read
Spring WebFlux Reactive CRUD REST API Example
Spring WebFlux can be defined as the reactive programming framework provided by the Spring ecosystem for the building of asynchronous, non-blocking, and event-driven applications and it can be designed to handle a large number of concurrent connections while consuming less resources. Key Terminologies:Mono and Flux: Mono can represent the publisher
5 min read
Basic Introduction to Spring WebFlux
Spring WebFlux is a reactive, non-blocking web framework that uses Project Reactor's reactive streams API to enable highly concurrent and asynchronous processing of web requests in a non-blocking and event-driven way. It is fully asynchronous and non-blocking using reactive streams and callbacks. It uses Project Reactor as the underlying reactive l
4 min read
Testing Spring WebFlux Reactive CRUD Rest APIs Using WebTestClient
Spring Boot is one of the famous frameworks for Back-end development. The Spring Boot Community Developed the Spring Reactive Web Framework. This Reactive Framework is available from Spring Boot 5.0 and Above versions only. The Spring Reactive allows developers to build Asynchronous, Non-Blocking, and Event-Driven Web Applications. When compared wi
8 min read
three90RightbarBannerImg