温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring boot2中webflux怎么使用

发布时间:2021-12-27 17:24:19 来源:亿速云 阅读:203 作者:iii 栏目:大数据

本篇内容主要讲解“spring boot2中webflux怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“spring boot2中webflux怎么使用”吧!

spring 5

Spring 5 是流行的 Spring 框架的下一个重大的版本升级。Spring 5 中最重要改动是把反应式编程的思想应用到了框架的各个方面,Spring 5 的反应式编程以 Reactor 库为基础。

WebFlux

WebFlux 模块的名称是 spring-webflux,名称中的 Flux 来源于 Reactor 中的类 Flux。该模块中包含了对反应式 HTTP、服务器推送事件和 WebSocket 的客户端和服务器端的支持。对于开发人员来说,比较重要的是服务器端的开发,

在服务器端,WebFlux 支持两种不同的编程模型:第一种是 Spring MVC 中使用的基于 Java 注解的方式;第二种是基于 Java 8 的 lambda 表达式的函数式编程模型。这两种编程模型只是在代码编写方式上存在不同。它们运行在同样的反应式底层架构之上,因此在运行时是相同的。WebFlux 需要底层提供运行时的支持,WebFlux 可以运行在支持 Servlet 3.1 非阻塞 IO API 的 Servlet 容器上,或是其他异步运行时环境,如 Netty 和 Undertow。

示例

新建工程

spring boot2中webflux怎么使用

加入webflux依赖:

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

新建类BasicController,加入如下代码:

/**
 * @author zhangfeng
 *
 */@RestControllerpublic class BasicController {    @GetMapping("/hello_world")
    public Mono<String> sayHelloWorld() {        return Mono.just("Hello World");
    }
}

BasicController 是 REST API 的控制器,通过@RestController 注解来声明。在 BasicController 中声明了一个 URI 为/hello_world 的映射。其对应的方法 sayHelloWorld()的返回值是 Mono<String>类型,其中包含的字符串"Hello World"会作为 HTTP 的响应内容。

测试

启动应用:

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.1.RELEASE)2018-04-16 17:32: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:32:37.006  INFO 10492 --- [           main] com.cloud.skyme.Websocket1Application    : No active profile set, falling back to default profiles: default2018-04-16 17:32: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:32:38.609  INFO 10492 --- [           main] s.w.r.r.m.a.RequestMappingHandlerMapping : Mapped "{[/hello_world],methods=[GET]}" onto public reactor.core.publisher.Mono<java.lang.String> com.cloud.skyme.BasicController.sayHelloWorld()2018-04-16 17:32: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:32:38.704  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 17:32: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:32:39.382  INFO 10492 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup2018-04-16 17:32:40.184  INFO 10492 --- [ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext     : Started HttpServer on /0:0:0:0:0:0:0:0:80802018-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:32:40.191  INFO 10492 --- [           main] com.cloud.skyme.Websocket1Application    : Started Websocket1Application in 3.672 seconds (JVM running for 4.772)2018-04-16 17:32: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 17:32:54.693  WARN 10492 --- [ctor-http-nio-2] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [GET http://localhost:8080/favicon.ico]: Response status 404

打开浏览器输入 http://localhost:8080/hello_world 可以看到返回结果。

到此,相信大家对“spring boot2中webflux怎么使用”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI