使用 Spring Boot 2.0 + WebFlux 實現 RESTful API

PedroSchill 6年前發布 | 38K 次閱讀 REST API Spring Boot

概述

什么是 Spring WebFlux, 它是一種異步的, 非阻塞的, 支持背壓(Back pressure)機制的Web 開發框架. 要深入了解 Spring WebFlux, 首先要了知道 Reactive Stream . 另一種編程姿勢, 和命令式編程相對的姿勢.

滾床單的姿勢有很多種, 目的都一樣.

WebFlux 支持兩種編程風(姿)格(勢)

  • 使用 @Controller 這種基于注解的姿勢, 與Sring MVC的姿勢相同
  • 基于Java 8 Lambda的函數式編程風格

注意: 上面只是兩種編程的姿勢, 和"普通話和重慶話都是中國話"是一個道理. 我們公司也有外地的, 對他我說普通話, 對本地同事說重慶話. 這叫多態

創建項目

通過 http://start.spring.io 創建項目骨架.

如果是手工配置, 需要添加Spring的里程碑(Milestone)倉庫:

<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url&gt;
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url&gt;
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

<pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url&gt; <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url&gt; <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories></pre>

測試

列舉所有用戶

創建用戶

獲取單個用戶

修改

刪除

源碼

demo-spring-boot-webflux-annotaion

 

來自:https://segmentfault.com/a/1190000012896148

 

 本文由用戶 PedroSchill 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!