Skip to content

Commit df79384

Browse files
committed
webflux test
1 parent 1635aac commit df79384

File tree

6 files changed

+146
-28
lines changed

6 files changed

+146
-28
lines changed

jdk-study/src/main/java/com/janloong/javabase/streamapi/sortString.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package com.janloong.javabase.streamapi;
1111

12+
import java.util.*;
1213
import java.util.stream.Stream;
1314

1415
/**

request/spring-demo/webflux.http

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
###
2-
GET http://localhost:8080/hello
2+
GET http://localhost:8080/home/hello
3+
4+
###
5+
GET http://localhost:8080/home/async
6+
7+
###
8+
GET http://localhost:8080/home/flux
9+
10+
###
11+
GET http://localhost:8080/home/flux2
312

413
###
514

spring-demo/spring-webflux/src/main/java/com/janloong/demo/controller/HomeController.java

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.janloong.demo.webflux.controller;
2+
3+
4+
import com.janloong.common.utils.ResponseResult;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
import reactor.core.publisher.Flux;
10+
import reactor.core.publisher.Mono;
11+
12+
/**
13+
* @author <a href ="mailto: janloongdoo@gmail.com">Janloong</a>
14+
* @date 2020-03-24 21:57
15+
*/
16+
@RestController
17+
@RequestMapping("/home")
18+
@Slf4j
19+
public class HomeController {
20+
21+
/**
22+
* @author <a href ="mailto: janloongdoo@gmail.com">Janloong</a>
23+
* @date 2020/3/24 0024 21:57
24+
**/
25+
@GetMapping("/hello")
26+
public Mono<ResponseResult> hello() {
27+
return Mono.just(ResponseResult.success("hello, webflux"));
28+
}
29+
30+
/**
31+
* @author <a href ="mailto: janloongdoo@gmail.com">Janloong</a>
32+
* @date 2020/3/25 10:08
33+
**/
34+
@RequestMapping("/async")
35+
//public Mono<ResponseResult> async() {
36+
public Mono<Object> async() {
37+
38+
return Mono.create(monoSink -> {
39+
log.info("mono创建");
40+
monoSink.success(ResponseResult.success("hello, mono created"));
41+
})
42+
.doOnSubscribe(subscription -> { //当订阅者去订阅发布者的时候,该方法会调用
43+
log.info("被订阅了?");
44+
log.info("{}", subscription);
45+
}).doOnNext(o -> { //当订阅者收到数据时,改方法会调用
46+
log.info("订阅者收到消息");
47+
log.info("{}", o);
48+
});
49+
}
50+
51+
/**
52+
* @author <a href ="mailto: janloongdoo@gmail.com">Janloong</a>
53+
* @date 2020/3/25 10:19
54+
**/
55+
@RequestMapping("/flux")
56+
public Flux<String> flux() {
57+
return Flux.just("hello", "webflux", "spring", "boot");
58+
}
59+
60+
/**
61+
* @author <a href ="mailto: janloongdoo@gmail.com">Janloong</a>
62+
* @date 2020/3/25 10:19
63+
**/
64+
@RequestMapping("/flux2")
65+
public Flux<ResponseResult> flux2() {
66+
return Flux.just(ResponseResult.success("hello"), ResponseResult.success("webflux"), ResponseResult.success("spring"), ResponseResult.success("boot"));
67+
}
68+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2020 All Rights Reserved.
3+
* ProjectName: cloud
4+
* FileName: HomeHandler.java
5+
* Author: janloongdoo@gmail.com
6+
* Website: www.janloong.com
7+
* Date: 2020/3/25 上午9:50
8+
* LastModify: 2020/3/25 上午9:50
9+
*/
10+
11+
package com.janloong.demo.webflux.controller.handler;
12+
13+
import org.springframework.http.MediaType;
14+
import org.springframework.web.reactive.function.BodyInserters;
15+
import org.springframework.web.reactive.function.server.ServerRequest;
16+
import org.springframework.web.reactive.function.server.ServerResponse;
17+
import reactor.core.publisher.Mono;
18+
19+
/**
20+
* @author <a href ="mailto: janloongdoo@gmail.com">Janloong</a>
21+
* @version V1.0
22+
* @date 2020-03-25 09:50
23+
**/
24+
//@Component
25+
public class HomeHandler {
26+
27+
public Mono<ServerResponse> helloHome(ServerRequest request) {
28+
return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN)
29+
.body(BodyInserters.fromObject("Hello, City!"));
30+
}
31+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2020 All Rights Reserved.
3+
* ProjectName: cloud
4+
* FileName: HomeRouter.java
5+
* Author: janloongdoo@gmail.com
6+
* Website: www.janloong.com
7+
* Date: 2020/3/25 上午9:54
8+
* LastModify: 2020/3/25 上午9:54
9+
*/
10+
11+
package com.janloong.demo.webflux.controller.router;
12+
13+
import com.janloong.demo.webflux.controller.handler.HomeHandler;
14+
import org.springframework.context.annotation.Bean;
15+
import org.springframework.http.MediaType;
16+
import org.springframework.web.reactive.function.server.RequestPredicates;
17+
import org.springframework.web.reactive.function.server.RouterFunction;
18+
import org.springframework.web.reactive.function.server.RouterFunctions;
19+
import org.springframework.web.reactive.function.server.ServerResponse;
20+
21+
/**
22+
* @author <a href ="mailto: janloongdoo@gmail.com">Janloong</a>
23+
* @version V1.0
24+
* @date 2020-03-25 09:54
25+
**/
26+
//@Configuration
27+
public class HomeRouter {
28+
29+
@Bean
30+
public RouterFunction<ServerResponse> routeCity(HomeHandler homeHandler) {
31+
return RouterFunctions
32+
.route(RequestPredicates.GET("/hello")
33+
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)),
34+
homeHandler::helloHome);
35+
}
36+
}

0 commit comments

Comments
 (0)