【spring boot】2.0 集成 webflux 以及 异步响应式新特性验证

〇·webflux新特性 这里不作介绍了,主要是演示一下集成,和异步响应式的测试。

一·引入jar包


   org.springframework.boot
   spring-boot-starter-parent
   2.0.0.RELEASE
    

   org.springframework.boot
   spring-boot-starter-webflux

二·代码编写

/**
 * 说明:
 *
 * @author WangBin
 * @version V1.0
 * @since 2018.03.08
 */
public class City {

    /**
     * 城市编号
     */
    private Long id;

    /**
     * 省份编号
     */
    private Long provinceId;

    /**
     * 城市名称
     */
    private String cityName;

    /**
     * 描述
     */
    private String description;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getProvinceId() {
        return provinceId;
    }

    public void setProvinceId(Long provinceId) {
        this.provinceId = provinceId;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "City{" +
                "id=" + id +
                ", provinceId=" + provinceId +
                ", cityName='" + cityName + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}

/**
 * 说明:
 *
 * @author WangBin
 * @version V1.0
 * @since 2018.03.08
 */
public interface CityService {

    /**
     * 获取城市信息列表
     *
     * @return
     */
    List 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);
}
/**
 * 说明:
 *
 * @author WangBin
 * @version V1.0
 * @since 2018.03.08
 */
@Service
public class CityServiceImpl implements CityService {
    
    // 模拟数据库,存储 City 信息
    private static Map CITY_DB = new HashMap<>();
    
    @Override
    public List findAllCity() {
        return new ArrayList<>(CITY_DB.values());
    }
    @Override
    public City findCityById(Long id) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.err.println("findOneCity 2");
        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;
    }
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * 说明:
 *
 * @author WangBin
 * @version V1.0
 * @since 2018.03.08
 */
@RestController
@RequestMapping(value = "/city")
public class CityRestController {
    @Autowired
    private CityService cityService;

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Mono findOneCity(@PathVariable("id") Long id) {
        System.err.println("findOneCity 1");
        Mono mono = Mono.create(cityMonoSink -> cityMonoSink.success(cityService.findCityById(id)));
        System.err.println("findOneCity 3");
        return mono;
    }

    /**
     * Flux 支持REST风格的 JSON 和 XML 序列化和反序列化
     * 处理函数,它将数据库中发现的所有 City 对象返回为JSON。
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Flux findAllCity() {
        System.err.println("findAllCity 1");
        Flux flux = Flux.create(cityFluxSink -> {
            cityService.findAllCity().forEach(city -> cityFluxSink.next(city));
            cityFluxSink.complete();
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.err.println("findAllCity 2");
        });
        System.err.println("findAllCity  end 3");
        return flux;
    }

    @RequestMapping(method = RequestMethod.POST)
    public Mono createCity(@RequestBody City city) {
        return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.saveCity(city)));
    }

    @RequestMapping(method = RequestMethod.PUT)
    public Mono modifyCity(@RequestBody City city) {
        return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.updateCity(city)));
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Mono modifyCity(@PathVariable("id") Long id) {
        return Mono.create(cityMonoSink -> cityMonoSink.success(cityService.deleteCity(id)));
    }
}
 
  

三·特性测试

执行 findAllCity 输出打印 可以看到 打印结果为  

findAllCity1

findAllCity3

findAllCity2 

Flux 所执行的方法 会 异步执行 ,执行完 1后 执行 3 最后等到 2 执行完毕后 return 结果。

你可能感兴趣的:(Java框架)