使用 Spring Boot 2.0 + WebFlux 实现 RESTful API

PedroSchill 6年前
   <h2>概述</h2>    <p>什么是 Spring WebFlux, 它是一种异步的, 非阻塞的, 支持背压(Back pressure)机制的Web 开发框架. 要深入了解 Spring WebFlux, 首先要了知道 Reactive Stream . 另一种编程姿势, 和命令式编程相对的姿势.</p>    <p>滚床单的姿势有很多种, 目的都一样.</p>    <h2>WebFlux 支持两种编程风(姿)格(势)</h2>    <ul>     <li>使用 @Controller 这种基于注解的姿势, 与Sring MVC的姿势相同</li>     <li>基于Java 8 Lambda的函数式编程风格</li>    </ul>    <p>注意: 上面只是两种编程的姿势, 和"普通话和重庆话都是中国话"是一个道理. 我们公司也有外地的, 对他我说普通话, 对本地同事说重庆话. 这叫多态</p>    <h2>创建项目</h2>    <p>通过 <a href="/misc/goto?guid=4958968112717629838" rel="nofollow,noindex">http://start.spring.io</a> 创建项目骨架.</p>    <p><img src="https://simg.open-open.com/show/106449454f090fa745775de64fb5e945.png"></p>    <p>如果是手工配置, 需要添加Spring的里程碑(Milestone)仓库:</p>    <pre>  <repositories>      <repository>          <id>spring-snapshots</id>          <name>Spring Snapshots</name>          <url>https://repo.spring.io/snapshot</url>          <snapshots>              <enabled>true</enabled>          </snapshots>      </repository>      <repository>          <id>spring-milestones</id>          <name>Spring Milestones</name>          <url>https://repo.spring.io/milestone</url>          <snapshots>              <enabled>false</enabled>          </snapshots>      </repository>  </repositories>    <pluginRepositories>      <pluginRepository>          <id>spring-snapshots</id>          <name>Spring Snapshots</name>          <url>https://repo.spring.io/snapshot</url>          <snapshots>              <enabled>true</enabled>          </snapshots>      </pluginRepository>      <pluginRepository>          <id>spring-milestones</id>          <name>Spring Milestones</name>          <url>https://repo.spring.io/milestone</url>          <snapshots>              <enabled>false</enabled>          </snapshots>      </pluginRepository>  </pluginRepositories></pre>    <h2>测试</h2>    <p>列举所有用户</p>    <p><img src="https://simg.open-open.com/show/26a470ac22bf76f9368f67088f0b2488.png"></p>    <p>创建用户</p>    <p><img src="https://simg.open-open.com/show/9a0b9f7cb97ecca66b5dbf57dcd4bbd4.png"></p>    <p>获取单个用户</p>    <p><img src="https://simg.open-open.com/show/e6522f5b8adc53718ddb6a183672c30c.png"></p>    <p>修改</p>    <p><img src="https://simg.open-open.com/show/319208d10f5ef891ec4153f0797bfa2d.png"></p>    <p>删除</p>    <p><img src="https://simg.open-open.com/show/72fab9c286e6dc0030fe3ef52142c6f0.png"></p>    <h2>源码</h2>    <p><a href="/misc/goto?guid=4959756556536743675" rel="nofollow,noindex">demo-spring-boot-webflux-annotaion</a></p>    <p> </p>    <p>来自:https://segmentfault.com/a/1190000012896148</p>    <p> </p>