添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
没读研的匕首  ·  Spring @Value ...·  3 月前    · 
呐喊的打火机  ·  Handling auto_now and ...·  3 月前    · 
八块腹肌的生菜  ·  C# ...·  3 年前    · 
一身肌肉的乌龙茶  ·  Qt ...·  3 年前    · 

// Tutorial //

Spring WebFlux - Spring Reactive Programming

Published on August 3, 2022
Default avatar

By Pankaj

Spring WebFlux - Spring Reactive Programming

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Spring WebFlux is the new module introduced in Spring 5. Spring WebFlux is the first step towards reactive programming model in spring framework.

Spring Reactive Programming

  • Reactive Manifesto
  • Reactive Streams
  • Java 9 Reactive Streams
  • RxJava
  • If you are new to Spring 5, please go through Spring 5 Features .

    Spring WebFlux

    Spring MVC module. Spring WebFlux is used to create fully asynchronous and non-blocking application built on event-loop execution model. Below diagram from Spring Official Documentation provides great insight on comparison of Spring WebFlux to Spring Web MVC. spring webflux and spring mvc If you are looking to develop a web application or Rest web service on non-blocking reactive model, then you can look into Spring WebFlux. Spring WebFlux is supported on Tomcat, Jetty, Servlet 3.1+ containers, as well as on non-Servlet runtimes such as Netty and Undertow. Spring WebFlux is built on Project Reactor . Project Reactor is the implementation of Reactive Streams specification. Reactor provides two types:

  • Mono : implements Publisher and returns 0 or 1 elements
  • Flux : implements Publisher and returns N elements.
  • Spring WebFlux Hello World Example

    Spring WebFlux Maven Dependencies

    Spring WebFlux Handler

    Spring WebFlux Router

    Spring Boot Application

    Java 9 Modules Support

    Running the Spring WebFlux Spring Boot App

    Spring WebFlux App Test

    REST web service.

    package com.journaldev.spring.client;
    import org.springframework.http.MediaType;
    import org.springframework.web.reactive.function.client.ClientResponse;
    import org.springframework.web.reactive.function.client.WebClient;
    import reactor.core.publisher.Mono;
    public class HelloWorldWebClient {
    	public static void main(String args[]) {
    		WebClient client = WebClient.create("https://localhost:8080");
    		Mono<ClientResponse> result = client.get()
    				.uri("/helloWorld")
    				.accept(MediaType.TEXT_PLAIN)
    				.exchange();
    			System.out.println("Result = " + result.flatMap(res -> res.bodyToMono(String.class)).block());
    

    Just run it as a simple java application and you should see the proper output with a lot of debug messages. spring reactive web WebClient example

    Summary

    GitHub Repository.

    Reference: Official Documentation

    Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

    Learn more about us


    About the authors
    Default avatar
    Pankaj

    author

    Still looking for an answer?

    Ask a question Search for more help

    Was this helpful?
    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    • August 30, 2019

    return Whitelabel Error Page, why ?

    - sunpiny

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    • July 17, 2019

    Nice post! I’ve one question, how do we deploy a Spring WebFlux project in production? If we use Spring WebFlux with Spring boot, we can’t create a WAR file which we could deploy in say tomcat. Do we have to create a fat jar file and run it using java -jar command?

    - Asraar

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    • March 12, 2019

    Thanks for this post! Simple and Neat webFlux starter app with unit test sample and webclient sample programs. Summary is cherry on cake for beginners.

    - Krishma