网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

springboot2中webflux怎么使用

本篇内容主要讲解“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依赖:


      org.springframework.boot
      spring-boot-starter-webflux
    

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

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

BasicController 是 REST API 的控制器,通过@RestController 注解来声明。在 BasicController 中声明了一个 URI 为/hello_world 的映射。其对应的方法 sayHelloWorld()的返回值是 Mono类型,其中包含的字符串"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 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怎么使用”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


当前文章:springboot2中webflux怎么使用
当前地址:http://bjjierui.cn/article/jcodci.html

其他资讯