Skip to content

Commit 6446896

Browse files
JeffLi1993liqiangqiang
authored andcommitted
Spring Boot 实现 WebFlux HTTP Restful 服务
1 parent 3f03be4 commit 6446896

File tree

7 files changed

+293
-0
lines changed

7 files changed

+293
-0
lines changed

springboot-webflux/pom.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<name>springboot-webflux :: Spring Boot 实现 WebFlux HTTP Restful 服务</name>
10+
11+
<!-- Spring Boot 启动父依赖 -->
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>2.0.0.M3</version>
16+
</parent>
17+
18+
<properties>
19+
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
20+
<mysql-connector>5.1.39</mysql-connector>
21+
</properties>
22+
23+
<dependencies>
24+
25+
<!-- Spring Boot Web Flux 依赖 -->
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-webflux</artifactId>
29+
</dependency>
30+
31+
<!-- Spring Boot Test 依赖 -->
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
38+
<!-- Junit -->
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
<version>4.12</version>
43+
</dependency>
44+
</dependencies>
45+
46+
47+
48+
<repositories>
49+
<repository>
50+
<id>spring-milestones</id>
51+
<name>Spring Milestones</name>
52+
<url>https://repo.spring.io/libs-milestone</url>
53+
<snapshots>
54+
<enabled>false</enabled>
55+
</snapshots>
56+
</repository>
57+
</repositories>
58+
59+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.spring.springboot.controller;
2+
3+
import org.spring.springboot.domain.City;
4+
import org.spring.springboot.service.CityService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.*;
7+
import reactor.core.publisher.Flux;
8+
import reactor.core.publisher.Mono;
9+
10+
/**
11+
* 城市 Controller 实现 Restful HTTP 服务
12+
* <p>
13+
* Created by bysocket on 09/29/2017.
14+
*/
15+
@RestController
16+
@RequestMapping(value = "/city")
17+
public class CityRestController {
18+
19+
@Autowired
20+
private CityService cityService;
21+
22+
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
23+
public Mono<City> findOneCity(@PathVariable("id") Long id) {
24+
return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.findCityById(id)));
25+
}
26+
27+
@RequestMapping(method = RequestMethod.GET)
28+
public Flux<City> findAllCity() {
29+
return Flux.create(cityFluxSink -> {
30+
cityService.findAllCity().forEach(city -> {
31+
cityFluxSink.next(city);
32+
});
33+
cityFluxSink.complete();
34+
});
35+
}
36+
37+
@RequestMapping(method = RequestMethod.POST)
38+
public Mono<Long> createCity(@RequestBody City city) {
39+
return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.saveCity(city)));
40+
}
41+
42+
@RequestMapping(method = RequestMethod.PUT)
43+
public Mono<Long> modifyCity(@RequestBody City city) {
44+
return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.updateCity(city)));
45+
}
46+
47+
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
48+
public Mono<Long> modifyCity(@PathVariable("id") Long id) {
49+
return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.deleteCity(id)));
50+
}
51+
}
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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.spring.springboot.service;
2+
3+
import org.spring.springboot.domain.City;
4+
5+
import java.util.List;
6+
7+
/**
8+
* 城市业务逻辑接口类
9+
*
10+
* Created by bysocket on 09/29/2017.
11+
*/
12+
public interface CityService {
13+
14+
/**
15+
* 获取城市信息列表
16+
*
17+
* @return
18+
*/
19+
List<City> findAllCity();
20+
21+
/**
22+
* 根据城市 ID,查询城市信息
23+
*
24+
* @param id
25+
* @return
26+
*/
27+
City findCityById(Long id);
28+
29+
/**
30+
* 新增城市信息
31+
*
32+
* @param city
33+
* @return
34+
*/
35+
Long saveCity(City city);
36+
37+
/**
38+
* 更新城市信息
39+
*
40+
* @param city
41+
* @return
42+
*/
43+
Long updateCity(City city);
44+
45+
/**
46+
* 根据城市 ID,删除城市信息
47+
*
48+
* @param id
49+
* @return
50+
*/
51+
Long deleteCity(Long id);
52+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.spring.springboot.service.impl;
2+
3+
import org.spring.springboot.domain.City;
4+
import org.spring.springboot.service.CityService;
5+
import org.springframework.stereotype.Service;
6+
7+
import java.util.ArrayList;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
/**
13+
* 城市业务逻辑实现类
14+
*
15+
* Created by bysocket on 09/29/2017.
16+
*/
17+
@Service
18+
public class CityServiceImpl implements CityService {
19+
20+
// 模拟数据库,存储 City 信息
21+
private static Map<Long, City> CITY_DB = new HashMap<>();
22+
23+
public List<City> findAllCity() {
24+
return new ArrayList<>(CITY_DB.values());
25+
}
26+
27+
public City findCityById(Long id) {
28+
return CITY_DB.get(id);
29+
}
30+
31+
@Override
32+
public Long saveCity(City city) {
33+
city.setId(CITY_DB.size() + 1L);
34+
CITY_DB.put(city.getId(), city);
35+
return city.getId();
36+
}
37+
38+
@Override
39+
public Long updateCity(City city) {
40+
CITY_DB.put(city.getId(), city);
41+
return city.getId();
42+
}
43+
44+
@Override
45+
public Long deleteCity(Long id) {
46+
CITY_DB.remove(id);
47+
return id;
48+
}
49+
50+
}

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

Whitespace-only changes.

0 commit comments

Comments
 (0)