Skip to content

Commit 5412e33

Browse files
author
liqiangqiang
committed
WebFlux 快速入门
1 parent fc52005 commit 5412e33

File tree

6 files changed

+164
-0
lines changed

6 files changed

+164
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

springboot-webflux-1-quickstart/src/main/resources/application.properties

Whitespace-only changes.

0 commit comments

Comments
 (0)