WebFlux之HelloWorld
此次开发工具使用idea,jdk11
-
打开idea
-
file -> new -> project 进入下面的界面,使用SpringInitialzr构建一个spirngboot项目
-
构建出一个基于webflux的项目
-
现在开始编写第一个helloworld
package com.example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
static final String HELLO_WORLD = "Hello World";
@GetMapping
public String hello(){
return HELLO_WORLD;
}
}
- 运行这个application
6.通过查看application启动监听的端口是8080,我们现在通过浏览器访问8080端口
可以到,helloworld已经成功显示了,但是上面的案例可以看出来,它好像和原来的mvc没什么区别,所以现在我们来改造一下这个项目,将代码修改成下面的样子
package com.example.controller;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.stream.IntStream;
@RestController
public class HelloWorldController {
static final String HELLO_WORLD = "Hello World";
@GetMapping
public String hello(){
return HELLO_WORLD;
}
/**
* 将数据转换成流的方式输出
* Mono是单条数据,Flux一般处理集合或者数组
* @return
*/
@GetMapping("/hello1")
public Mono<String> hello1(){
return Mono.just(HELLO_WORLD);
}
/**
* MediaType.TEXT_EVENT_STREAM_VALUE 设置这个头部是让flux变成响应式的
* 如果不设置这个参数,flux就相当于List Array
* @return
*/
@GetMapping(value = "/hellon",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> hellon(){
return Flux
.fromStream(IntStream.range(1,20).mapToObj(item ->HELLO_WORLD + item))
.doOnNext(item -> {
try {
//等待500毫秒响应一次数据
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
/**
* 定义一个路由函数
* @return
*/
@Bean
public RouterFunction<ServerResponse> function(){
return RouterFunctions.route(
RequestPredicates.GET("/helloF"),
serverRequest -> {
return ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON).bodyValue(HELLO_WORLD);
}
);
}
}
通过改造之后,发现webflux有不同方式响应数据,下一次,我会讲webflux如何整合thymeleaf,实现一个登陆注册个功能