mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-13 21:43:45 +08:00
WebFlux Web CRUD 实践
This commit is contained in:
@@ -1,51 +0,0 @@
|
||||
package org.spring.springboot.controller;
|
||||
|
||||
import org.spring.springboot.domain.City;
|
||||
import org.spring.springboot.service.CityService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* 城市 Controller 实现 Restful HTTP 服务
|
||||
* <p>
|
||||
* Created by bysocket on 09/29/2017.
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(value = "/city")
|
||||
public class CityRestController {
|
||||
|
||||
@Autowired
|
||||
private CityService cityService;
|
||||
|
||||
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
||||
public Mono<City> findOneCity(@PathVariable("id") Long id) {
|
||||
return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.findCityById(id)));
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
public Flux<City> findAllCity() {
|
||||
return Flux.create(cityFluxSink -> {
|
||||
cityService.findAllCity().forEach(city -> {
|
||||
cityFluxSink.next(city);
|
||||
});
|
||||
cityFluxSink.complete();
|
||||
});
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST)
|
||||
public Mono<Long> createCity(@RequestBody City city) {
|
||||
return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.saveCity(city)));
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.PUT)
|
||||
public Mono<Long> modifyCity(@RequestBody City city) {
|
||||
return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.updateCity(city)));
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
|
||||
public Mono<Long> modifyCity(@PathVariable("id") Long id) {
|
||||
return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.deleteCity(id)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.spring.springboot.dao;
|
||||
|
||||
import org.spring.springboot.domain.City;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@Repository
|
||||
public class CityRepository {
|
||||
|
||||
private ConcurrentMap<Long, City> repository = new ConcurrentHashMap<>();
|
||||
|
||||
private static final AtomicLong idGenerator = new AtomicLong(0);
|
||||
|
||||
public Long save(City city) {
|
||||
Long id = idGenerator.incrementAndGet();
|
||||
city.setId(id);
|
||||
repository.put(id, city);
|
||||
return id;
|
||||
}
|
||||
|
||||
public Collection<City> findAll() {
|
||||
return repository.values();
|
||||
}
|
||||
|
||||
|
||||
public City findCityById(Long id) {
|
||||
return repository.get(id);
|
||||
}
|
||||
|
||||
public Long updateCity(City city) {
|
||||
repository.put(city.getId(), city);
|
||||
return city.getId();
|
||||
}
|
||||
|
||||
public Long deleteCity(Long id) {
|
||||
repository.remove(id);
|
||||
return id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package org.spring.springboot.handler;
|
||||
|
||||
import org.spring.springboot.dao.CityRepository;
|
||||
import org.spring.springboot.domain.City;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class CityHandler {
|
||||
|
||||
private final CityRepository cityRepository;
|
||||
|
||||
@Autowired
|
||||
public CityHandler(CityRepository cityRepository) {
|
||||
this.cityRepository = cityRepository;
|
||||
}
|
||||
|
||||
public Mono<Long> save(City city) {
|
||||
return Mono.create(cityMonoSink -> cityMonoSink.success(cityRepository.save(city)));
|
||||
}
|
||||
|
||||
public Mono<City> findCityById(Long id) {
|
||||
return Mono.create(cityMonoSink -> cityMonoSink.success(cityRepository.findCityById(id)));
|
||||
}
|
||||
|
||||
public Flux<City> findAllCity() {
|
||||
return Flux.create(cityFluxSink -> {
|
||||
cityRepository.findAll().forEach(city -> cityFluxSink.next(city));
|
||||
cityFluxSink.complete();
|
||||
});
|
||||
}
|
||||
|
||||
public Mono<Long> modifyCity(City city) {
|
||||
return Mono.create(cityMonoSink -> cityMonoSink.success(cityRepository.updateCity(city)));
|
||||
}
|
||||
|
||||
public Mono<Long> deleteCity(Long id) {
|
||||
return Mono.create(cityMonoSink -> cityMonoSink.success(cityRepository.deleteCity(id)));
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package org.spring.springboot.service;
|
||||
|
||||
import org.spring.springboot.domain.City;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 城市业务逻辑接口类
|
||||
*
|
||||
* Created by bysocket on 09/29/2017.
|
||||
*/
|
||||
public interface CityService {
|
||||
|
||||
/**
|
||||
* 获取城市信息列表
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<City> findAllCity();
|
||||
|
||||
/**
|
||||
* 根据城市 ID,查询城市信息
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
City findCityById(Long id);
|
||||
|
||||
/**
|
||||
* 新增城市信息
|
||||
*
|
||||
* @param city
|
||||
* @return
|
||||
*/
|
||||
Long saveCity(City city);
|
||||
|
||||
/**
|
||||
* 更新城市信息
|
||||
*
|
||||
* @param city
|
||||
* @return
|
||||
*/
|
||||
Long updateCity(City city);
|
||||
|
||||
/**
|
||||
* 根据城市 ID,删除城市信息
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
Long deleteCity(Long id);
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package org.spring.springboot.service.impl;
|
||||
|
||||
import org.spring.springboot.domain.City;
|
||||
import org.spring.springboot.service.CityService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 城市业务逻辑实现类
|
||||
*
|
||||
* Created by bysocket on 09/29/2017.
|
||||
*/
|
||||
@Service
|
||||
public class CityServiceImpl implements CityService {
|
||||
|
||||
// 模拟数据库,存储 City 信息
|
||||
private static Map<Long, City> CITY_DB = new HashMap<>();
|
||||
|
||||
public List<City> findAllCity() {
|
||||
return new ArrayList<>(CITY_DB.values());
|
||||
}
|
||||
|
||||
public City findCityById(Long id) {
|
||||
return CITY_DB.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long saveCity(City city) {
|
||||
city.setId(CITY_DB.size() + 1L);
|
||||
CITY_DB.put(city.getId(), city);
|
||||
return city.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long updateCity(City city) {
|
||||
CITY_DB.put(city.getId(), city);
|
||||
return city.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long deleteCity(Long id) {
|
||||
CITY_DB.remove(id);
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.spring.springboot.webflux.controller;
|
||||
|
||||
import org.spring.springboot.domain.City;
|
||||
import org.spring.springboot.handler.CityHandler;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/city")
|
||||
public class CityWebFluxController {
|
||||
|
||||
@Autowired
|
||||
private CityHandler cityHandler;
|
||||
|
||||
@GetMapping(value = "/{id}")
|
||||
public Mono<City> findCityById(@PathVariable("id") Long id) {
|
||||
return cityHandler.findCityById(id);
|
||||
}
|
||||
|
||||
@GetMapping()
|
||||
public Flux<City> findAllCity() {
|
||||
return cityHandler.findAllCity();
|
||||
}
|
||||
|
||||
@PostMapping()
|
||||
public Mono<Long> saveCity(@RequestBody City city) {
|
||||
return cityHandler.save(city);
|
||||
}
|
||||
|
||||
@PutMapping()
|
||||
public Mono<Long> modifyCity(@RequestBody City city) {
|
||||
return cityHandler.modifyCity(city);
|
||||
}
|
||||
|
||||
@DeleteMapping(value = "/{id}")
|
||||
public Mono<Long> deleteCity(@PathVariable("id") Long id) {
|
||||
return cityHandler.deleteCity(id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user