In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use webflux in spring boot2". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use webflux in spring boot2.
Spring 5
Spring 5 is the next major version upgrade of the popular Spring framework. The most important change in Spring 5 is the application of the idea of reactive programming to all aspects of the framework. Spring 5's reactive programming is based on the Reactor library.
WebFlux
The name of the WebFlux module is spring-webflux, and the Flux in the name comes from the class Flux in Reactor. This module includes client-and server-side support for reactive HTTP, server push events, and WebSocket. For developers, what is more important is server-side development
On the server side, WebFlux supports two different programming models: the first is based on Java annotations used in Spring MVC, and the second is a functional programming model based on Java 8 lambda expressions. The two programming models are just different in the way the code is written. They run on the same reactive underlying architecture, so they are the same at runtime. WebFlux requires runtime support from the underlying, and WebFlux can run on Servlet containers that support Servlet 3.1 non-blocking IO API, or other asynchronous runtime environments such as Netty and Undertow.
Example
Newly built project
Join the webflux dependency:
Org.springframework.boot spring-boot-starter-webflux
Create a new class BasicController and add the following code:
/ * * @ author zhangfeng * * / @ RestControllerpublic class BasicController {@ GetMapping ("/ hello_world") public Mono sayHelloWorld () {return Mono.just ("HelloWorld");}}
BasicController is the controller of REST API, declared through the @ RestController annotation. A mapping whose URI is / hello_world is declared in BasicController. The return value of its corresponding method sayHelloWorld () is of type Mono, where the string "HelloWorld" is used as the response content of the HTTP.
test
Start the application:
. _ _ / / _ _ _ (() _ _ _ |'_ _ _ | |\ / _ _ _ | | | (_ _ | |) )'| _ |. _ _ | _ | | _ | _ | | _\ _ _ | | / = | _ | = | _ _ / = / _ /:: Spring Boot:: (v2.0.1.RELEASE) 2018-04-16 1715 INFO 36.993 INFO 10492-[main] com.cloud.skyme.Websocket1Application: Starting Websocket1Application on DESKTOP-E3I9LR5 with PID 10492 (C:\ java\ workspace\ weixin\ websocket-1\ target\ classes started by zhangfeng in C:\ java\ workspace\ weixin\ websocket- 1) 2018-04-16 17 com.cloud.skyme.Websocket1Application 32 com.cloud.skyme.Websocket1Application 37.006 INFO 10492-[main] com.cloud.skyme.Websocket1Application: No active profile set Falling back to default profiles: default2018-04-16 17 INFO 37.089 INFO 10492-[main] onfigReactiveWebServerApplicationContext: Refreshing org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@7a52f2a2: startup date [Mon Apr 16 17:32:37 CST 2018] Root of context hierarchy2018-04-16 17 main 32 s.w.r.r.m.a.RequestMappingHandlerMapping 38.609 INFO 10492-[main] s.w.r.r.m.a.RequestMappingHandlerMapping: Mapped "{[/ hello_world] Methods= [GET]} "onto public reactor.core.publisher.Mono com.cloud.skyme.BasicController.sayHelloWorld () 2018-04-16 17 INFO 38.704 INFO 10492-[main] o.s.w.r.handler.SimpleUrlHandlerMapping: Mapped URL path [/ webjars/**] onto handler of type [class org.springframework.web.reactive.resource.ResourceWebHandler] 2018-04-16 17 onto public reactor.core.publisher.Mono com.cloud.skyme.BasicController.sayHelloWorld 32 INFO 10492-[main] o .s.w.r.handler.SimpleUrlHandlerMapping: Mapped URL path [/ * *] onto handler of type [class org.springframework.web.reactive.resource.ResourceWebHandler] 2018-04-16 17purse 32 INFO 38.885 INFO 10492-[main] o.s.w.r.r.m.a.ControllerMethodResolver: Looking for @ ControllerAdvice: org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@7a52f2a2: startup date [Mon Apr 16 17:32:37 CST 2018] Root of context hierarchy2018-04-16 17 main 32 root of context hierarchy2018 39.382 INFO 10492-[main] o.s.j.e.a.AnnotationMBeanExporter: Registering beans for JMX exposure on startup2018-04-16 1715 32 o.s.j.e.a.AnnotationMBeanExporter 40.184 INFO 10492-[ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext: Started HttpServer on / 0VOV0VOV0V0V0V0V0VOVOGULO VOUL0VULO 802018-04-16 17:32:40 .184 INFO 10492-[main] o.s.b.web.embedded.netty.NettyWebServer: Netty started on port (s): 80802018-04-16 17 o.s.b.web.embedded.netty.NettyWebServer 40.191 INFO 10492-[main] com.cloud.skyme.Websocket1Application: Started Websocket1Application in 3.672 seconds (JVM running for 4.772) 2018-04-16 17 Netty started on port 32 Netty started on port 54.559 WARN 10492-- [ctor-http-nio-2] .a .w.r.e.DefaultErrorWebExceptionHandler: Failed to handle request [GET http://localhost:8080/helloworld]: Response status 4042018-04-16 1717Visual32Failed to handle request 54.693 WARN 10492-[ctor-http-nio-2] .a.w.r.DefaultErrorWebExceptionHandler: Failed to handle request [GET ErrorWebExceptionHandler
Open the browser and enter http://localhost:8080/hello_world to see the returned result.
At this point, I believe you have a deeper understanding of "how to use webflux in spring boot2". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.