File tree Expand file tree Collapse file tree 6 files changed +164
-0
lines changed
springboot-webflux-1-quickstart
java/org/spring/springboot Expand file tree Collapse file tree 6 files changed +164
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
3
+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
4
+ <modelVersion >4.0.0</modelVersion >
5
+
6
+ <groupId >springboot</groupId >
7
+ <artifactId >springboot-webflux-1-quickstart</artifactId >
8
+ <version >0.0.1-SNAPSHOT</version >
9
+ <name >springboot-webflux-1-quickstart :: Spring Boot WebFlux 快速入门</name >
10
+
11
+ <!-- Spring Boot 启动父依赖 -->
12
+ <parent >
13
+ <groupId >org.springframework.boot</groupId >
14
+ <artifactId >spring-boot-starter-parent</artifactId >
15
+ <version >2.0.1.RELEASE</version >
16
+ </parent >
17
+
18
+ <dependencies >
19
+
20
+ <!-- Spring Boot Web Flux 依赖 -->
21
+ <dependency >
22
+ <groupId >org.springframework.boot</groupId >
23
+ <artifactId >spring-boot-starter-webflux</artifactId >
24
+ </dependency >
25
+
26
+ <!-- Spring Boot Test 依赖 -->
27
+ <dependency >
28
+ <groupId >org.springframework.boot</groupId >
29
+ <artifactId >spring-boot-starter-test</artifactId >
30
+ <scope >test</scope >
31
+ </dependency >
32
+
33
+ <!-- Junit -->
34
+ <dependency >
35
+ <groupId >junit</groupId >
36
+ <artifactId >junit</artifactId >
37
+ <version >4.12</version >
38
+ </dependency >
39
+ </dependencies >
40
+
41
+ </project >
Original file line number Diff line number Diff line change
1
+ package org .spring .springboot ;
2
+
3
+ import org .springframework .boot .SpringApplication ;
4
+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
5
+
6
+ /**
7
+ * Spring Boot 应用启动类
8
+ *
9
+ * Created by bysocket on 09/29/2017.
10
+ */
11
+ // Spring Boot 应用的标识
12
+ @ SpringBootApplication
13
+ public class Application {
14
+
15
+ public static void main (String [] args ) {
16
+ // 程序启动入口
17
+ // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
18
+ SpringApplication .run (Application .class ,args );
19
+
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ package org .spring .springboot .domain ;
2
+
3
+ /**
4
+ * 城市实体类
5
+ *
6
+ * Created by bysocket on 09/29/2017.
7
+ */
8
+ public class City {
9
+
10
+ /**
11
+ * 城市编号
12
+ */
13
+ private Long id ;
14
+
15
+ /**
16
+ * 省份编号
17
+ */
18
+ private Long provinceId ;
19
+
20
+ /**
21
+ * 城市名称
22
+ */
23
+ private String cityName ;
24
+
25
+ /**
26
+ * 描述
27
+ */
28
+ private String description ;
29
+
30
+ public Long getId () {
31
+ return id ;
32
+ }
33
+
34
+ public void setId (Long id ) {
35
+ this .id = id ;
36
+ }
37
+
38
+ public Long getProvinceId () {
39
+ return provinceId ;
40
+ }
41
+
42
+ public void setProvinceId (Long provinceId ) {
43
+ this .provinceId = provinceId ;
44
+ }
45
+
46
+ public String getCityName () {
47
+ return cityName ;
48
+ }
49
+
50
+ public void setCityName (String cityName ) {
51
+ this .cityName = cityName ;
52
+ }
53
+
54
+ public String getDescription () {
55
+ return description ;
56
+ }
57
+
58
+ public void setDescription (String description ) {
59
+ this .description = description ;
60
+ }
61
+ }
Original file line number Diff line number Diff line change
1
+ package org .spring .springboot .handler ;
2
+
3
+ import org .springframework .http .MediaType ;
4
+ import org .springframework .stereotype .Component ;
5
+ import org .springframework .web .reactive .function .BodyInserters ;
6
+ import org .springframework .web .reactive .function .server .ServerRequest ;
7
+ import org .springframework .web .reactive .function .server .ServerResponse ;
8
+ import reactor .core .publisher .Mono ;
9
+
10
+ @ Component
11
+ public class CityHandler {
12
+
13
+ public Mono <ServerResponse > helloCity (ServerRequest request ) {
14
+ return ServerResponse .ok ().contentType (MediaType .TEXT_PLAIN )
15
+ .body (BodyInserters .fromObject ("Hello, City!" ));
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ package org .spring .springboot .router ;
2
+
3
+ import org .spring .springboot .handler .CityHandler ;
4
+ import org .springframework .context .annotation .Bean ;
5
+ import org .springframework .context .annotation .Configuration ;
6
+ import org .springframework .http .MediaType ;
7
+ import org .springframework .web .reactive .function .server .RequestPredicates ;
8
+ import org .springframework .web .reactive .function .server .RouterFunction ;
9
+ import org .springframework .web .reactive .function .server .RouterFunctions ;
10
+ import org .springframework .web .reactive .function .server .ServerResponse ;
11
+
12
+ @ Configuration
13
+ public class CityRouter {
14
+
15
+
16
+ @ Bean
17
+ public RouterFunction <ServerResponse > routeCity (CityHandler cityHandler ) {
18
+ return RouterFunctions
19
+ .route (RequestPredicates .GET ("/hello" )
20
+ .and (RequestPredicates .accept (MediaType .TEXT_PLAIN )),
21
+ cityHandler ::helloCity );
22
+ }
23
+
24
+ }
You can’t perform that action at this time.
0 commit comments