日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot 2 快速教程:WebFlux Restful CRUD 实践(三)

發布時間:2025/5/22 javascript 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot 2 快速教程:WebFlux Restful CRUD 实践(三) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
摘要: 原創出處 https://www.bysocket.com 「公眾號:泥瓦匠BYSocket 」歡迎關注和轉載,保留摘要,謝謝!

這是泥瓦匠的第102篇原創

03:WebFlux Web CRUD 實踐

文章工程:
* JDK 1.8
* Maven 3.5.2
* Spring Boot 2.1.3.RELEASE
* 工程名:springboot-webflux-2-restful
* 工程地址:見文末

一、前言

上一篇基于功能性端點去創建一個簡單服務,實現了 Hello 。這一篇用 Spring Boot WebFlux 的注解控制層技術創建一個 CRUD WebFlux 應用,讓開發更方便。這里我們不對數據庫儲存進行訪問,因為后續會講到,而且這里主要是講一個完整的 WebFlux CRUD。

二、結構

這個工程會對城市(City)進行管理實現 CRUD 操作。該工程創建編寫后,得到下面的結構,其目錄結構如下:

├── pom.xml ├── src │?? └── main │?? ├── java │?? │?? └── org │?? │?? └── spring │?? │?? └── springboot │?? │?? ├── Application.java │?? │?? ├── dao │?? │?? │?? └── CityRepository.java │?? │?? ├── domain │?? │?? │?? └── City.java │?? │?? ├── handler │?? │?? │?? └── CityHandler.java │?? │?? └── webflux │?? │?? └── controller │?? │?? └── CityWebFluxController.java │?? └── resources │?? └── application.properties └── target

如目錄結構,我們需要編寫的內容按順序有:

  • 對象
  • 數據訪問層類 Repository
  • 處理器類 Handler
  • 控制器類 Controller

三、對象

新建包 org.spring.springboot.domain ,作為編寫城市實體對象類。新建城市(City)對象 City,代碼如下:

/*** 城市實體類**/ 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; } }

城市包含了城市編號、省份編號、城市名稱和描述。具體開發中,會使用 Lombok 工具來消除冗長的 Java 代碼,尤其是 POJO 的 getter / setter 方法。具體查看 Lombok 官網地址:projectlombok.org。

四、數據訪問層 CityRepository

新建包 org.spring.springboot.dao ,作為編寫城市數據訪問層類 Repository。新建 CityRepository,代碼如下:

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; } }

@Repository?用于標注數據訪問組件,即 DAO 組件。實現代碼中使用名為?repository?的 Map 對象作為內存數據存儲,并對對象具體實現了具體業務邏輯。CityRepository?負責將 Book 持久層(數據操作)相關的封裝組織,完成新增、查詢、刪除等操作。

這里不會涉及到數據存儲這塊,具體數據存儲會在后續介紹。

五、處理器類 Handler

新建包 org.spring.springboot.handler ,作為編寫城市處理器類 CityHandler。新建 CityHandler,代碼如下:

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.justOrEmpty(cityRepository.findCityById(id)); } public Flux<City> findAllCity() { return Flux.fromIterable(cityRepository.findAll()); } 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))); } }

@Component?泛指組件,當組件不好歸類的時候,使用該注解進行標注。然后用?final?和?@Autowired?標注在構造器注入 CityRepository Bean,代碼如下:

private final CityRepository cityRepository;@Autowiredpublic CityHandler(CityRepository cityRepository) { this.cityRepository = cityRepository; }

從返回值可以看出,Mono 和 Flux 適用于兩個場景,即:

  • Mono:實現發布者,并返回 0 或 1 個元素,即單對象
  • Flux:實現發布者,并返回 N 個元素,即 List 列表對象

有人會問,這為啥不直接返回對象,比如返回 City/Long/List。原因是,直接使用 Flux 和 Mono 是非阻塞寫法,相當于回調方式。利用函數式可以減少了回調,因此會看不到相關接口。反應了是 WebFlux 的好處:集合了非阻塞 + 異步。

5.1 Mono

Mono 是什么? 官方描述如下:A Reactive Streams Publisher with basic rx operators that completes successfully by emitting an element, or with an error.

Mono 是響應流 Publisher ,即要么成功發布元素,要么錯誤。如圖所示:

Mono 常用的方法有:

  • Mono.create():使用 MonoSink 來創建 Mono
  • Mono.justOrEmpty():從一個 Optional 對象或 null 對象中創建 Mono。
  • Mono.error():創建一個只包含錯誤消息的 Mono
  • Mono.never():創建一個不包含任何消息通知的 Mono
  • Mono.delay():在指定的延遲時間之后,創建一個 Mono,產生數字 0 作為唯一值

5.2 Flux

Flux 是什么? 官方描述如下:A Reactive Streams Publisher with rx operators that emits 0 to N elements, and then completes (successfully or with an error).

Flux 是響應流 Publisher ,即要么成功發布 0 到 N 個元素,要么錯誤。Flux 其實是 Mono 的一個補充。如圖所示:

所以要注意:如果知道 Publisher 是 0 或 1 個,則用 Mono。

Flux 最值得一提的是 fromIterable 方法。fromIterable(Iterable<? extends T> it) 可以發布 Iterable 類型的元素。當然,Flux 也包含了基礎的操作:map、merge、concat、flatMap、take,這里就不展開介紹了。

六、控制器類 Controller

Spring Boot WebFlux 也可以使用自動配置加注解驅動的模式來進行開發。

新建包目錄?org.spring.springboot.webflux.controller?,并在目錄中創建名為 CityWebFluxController 來處理不同的 HTTP Restful 業務請求。代碼如下:

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); } }

這里按照 REST 風格實現接口。那具體什么是 REST?

REST 是屬于 WEB 自身的一種架構風格,是在 HTTP 1.1 規范下實現的。Representational State Transfer 全稱翻譯為表現層狀態轉化。Resource:資源。比如 newsfeed;Representational:表現形式,比如用JSON,富文本等;State Transfer:狀態變化。通過HTTP 動作實現。

理解 REST ,要明白五個關鍵要素:

  • 資源(Resource)
  • 資源的表述(Representation)
  • 狀態轉移(State Transfer)
  • 統一接口(Uniform Interface)
  • 超文本驅動(Hypertext Driven)

6 個主要特性:

  • 面向資源(Resource Oriented)
  • 可尋址(Addressability)
  • 連通性(Connectedness)
  • 無狀態(Statelessness)
  • 統一接口(Uniform Interface)
  • 超文本驅動(Hypertext Driven)

具體這里就不一一展開,詳見 http://www.infoq.com/cn/articles/understanding-restful-style。

請求入參、Filters、重定向、Conversion、formatting 等知識會和以前 MVC 的知識一樣,詳情見文檔:
https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html

七、運行工程

一個 CRUD 的 Spring Boot Webflux 工程就開發完畢了,下面運行工程驗證下。使用 IDEA 右側工具欄,點擊 Maven Project Tab ,點擊使用下 Maven 插件的?install?命令。或者使用命令行的形式,在工程根目錄下,執行 Maven 清理和安裝工程的指令:

cd springboot-webflux-2-restful mvn clean install

在控制臺中看到成功的輸出:

... 省略 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:30 min [INFO] Finished at: 2017-10-15T10:00:54+08:00 [INFO] Final Memory: 31M/174M [INFO] ------------------------------------------------------------------------

在 IDEA 中執行?Application?類啟動,任意正常模式或者 Debug 模式??梢栽诳刂婆_看到成功運行的輸出:

... 省略 2018-04-10 08:43:39.932 INFO 2052 --- [ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext : Started HttpServer on /0:0:0:0:0:0:0:0:8080 2018-04-10 08:43:39.935 INFO 2052 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 8080 2018-04-10 08:43:39.960 INFO 2052 --- [ main] org.spring.springboot.Application : Started Application in 6.547 seconds (JVM running for 9.851)

打開 POST MAN 工具,開發必備。進行下面操作:

新增城市信息 POST http://127.0.0.1:8080/city

獲取城市信息列表 GET http://127.0.0.1:8080/city

其他接口就不演示了。

八、總結

這里,探討了 Spring WebFlux 的一些功能,構建沒有底層數據庫的基本 CRUD 工程。為了更好的展示了如何創建 Flux 流,以及如何對其進行操作。下面會講到如何操作數據存儲。

系列教程目錄

  • 《01:WebFlux 系列教程大綱》
  • 《02:WebFlux 快速入門實踐》
  • 《03:WebFlux Web CRUD 實踐》
  • 《04:WebFlux 整合 Mongodb》
  • 《05:WebFlux 整合 Thymeleaf》
  • 《06:WebFlux 中 Thymeleaf 和 Mongodb 實踐》
  • 《07:WebFlux 整合 Redis》
  • 《08:WebFlux 中 Redis 實現緩存》
  • 《09:WebFlux 中 WebSocket 實現通信》
  • 《10:WebFlux 集成測試及部署》
  • 《11:WebFlux 實戰圖書管理系統》

代碼示例

本文示例讀者可以通過查看下面倉庫的中的模塊工程名: 2-x-spring-boot-webflux-handling-errors:

  • Github:https://github.com/JeffLi1993/springboot-learning-example
  • Gitee:https://gitee.com/jeff1993/springboot-learning-example

如果您對這些感興趣,歡迎 star、follow、收藏、轉發給予支持!

參考資料

  • Spring Boot 2.x WebFlux 系列:https://www.bysocket.com/archives/2290
  • spring.io 官方文檔

以下專題教程也許您會有興趣

  • 《Spring Boot 2.x 系列教程》?https://www.bysocket.com/springboot
  • 《Java 核心系列教程》?https://www.bysocket.com/archives/2100
?
(關注微信公眾號,領取 Java 精選干貨學習資料)?
(添加我微信:bysocket01。加入純技術交流群,成長技術)

轉載于:https://www.cnblogs.com/Alandre/p/10877982.html

總結

以上是生活随笔為你收集整理的Spring Boot 2 快速教程:WebFlux Restful CRUD 实践(三)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产一区二区自拍 | 亚洲精品二区 | 日韩精品一区二区亚洲av性色 | 91www在线观看 | 人物动物互动39集免费观看 | 国内自拍视频在线观看 | 人人妻人人澡人人爽久久av | 欧美性生交xxxxx久久久 | 精品国产一区二区三区久久狼黑人 | 日本激情小视频 | 国产精品自产拍在线观看 | 中文字幕av网址 | 91粉色视频| av毛片在线看 | 久久免费看片 | 久久在线视频精品 | 艹男人的日日夜夜 | 91久久国产精品 | 国产成人免费看一级大黄 | 91福利视频导航 | 亚洲综合黄色 | 日日夜夜国产精品 | 亚洲一区二区日本 | 成人免费视屏 | 亚洲欧美精品一区 | 亚洲三级图片 | 麻豆黄色片 | 成人免费视频大全 | 天天色天天干天天 | 久久免费片| 国产精品久久久久久久久久久新郎 | 免费看欧美大片 | 国产女人水真多18毛片18精品 | 草草视频在线观看 | 久久久久久久穴 | 久久久久久亚洲精品 | 七月色| 日本在线视频不卡 | 中文字幕免费观看视频 | 黄色一级免费网站 | 中文日韩 | 亚洲一区久久久 | 国产精品成人一区二区网站软件 | 亚洲日日夜夜 | 在线国产91 | 精品日韩欧美 | 国产不卡视频 | 91免费福利视频 | 91午夜视频| 亚洲成人免费看 | 欧美区二区三区 | 男生和女生差差视频 | 大尺度床戏视频 | 免费看h网站 | 欧美成人一区二区视频 | 成人在线精品视频 | 国产一区美女 | 午夜免费影院 | 天天干天天干 | 靠逼在线观看 | 色就是色av | 操亚洲美女| 国产资源在线免费观看 | 91一区二区视频 | 成年人在线视频网站 | 综合中文字幕 | 九九九网站 | 国家队动漫免费观看在线观看晨光 | 国产精品污网站 | 麻豆出品 | 国产1区在线 | 婷婷综合六月 | 日批视频在线免费看 | 黄色亚洲网站 | 永久免费成人 | 日本三区视频 | 情趣五月天 | 香蕉在线影院 | 亚洲一区二区三区四区 | 天天操天天操天天操天天操 | 亚洲国产精品久久久久爰性色 | 九色九一 | 欧美另类videossexo高潮 | 日韩欧美视频一区二区三区 | 婷婷丁香六月天 | 精品香蕉视频 | 亚洲视频你懂的 | 成人免费av在线 | 久草网站 | 色哟哟在线 | 国产精品黄色大片 | 成人免费视频网站 | 成年免费在线观看 | 精品国产乱码久久久久久久 | 国产青草视频在线观看 | jizz国产精品 | 亚洲美女性生活 | 伊人色网 | 欧美精品一区二区三区在线 |