【SpringBoot】集成 Web Flux

来源:https://www.cnblogs.com/EddieBlog/archive/2018/06/08/9157496.html
-Advertisement-
Play Games

必需學會SpringBoot基礎知識 Takes an opinionated view of building production-ready Spring applications. Spring Boot favors convention over configuration and is ...


前言:

必需學會SpringBoot基礎知識

簡介:

Takes an opinionated view of building production-ready Spring applications. Spring Boot favors convention over configuration and is designed to get you up and running as quickly as possible.

工具:

JDK8

apache-maven-3.5.2

IntelliJ IDEA 2018.1.3 x64

 

(1)新建一個springboot工程
(2)pom.xml
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
(3)建立實體 User (簡化不寫了)
(4)建立 repository
package com.lwc.repository;

import com.lwc.pojo.User;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author eddie.lee
 * @Package com.lwc.repository
 * @ClassName UserRepository
 * @description this is dao
 * @date created in 2018-06-07 21:36
 * @modified by
 */
@Repository
public class UserRespository {

    /**
     * 使用記憶體方式存儲 ===》 Map
     */
    private ConcurrentMap<Integer, User> repository = new ConcurrentHashMap<>();

    /**
     * id生成
     */
    private final static AtomicInteger idGenerator = new AtomicInteger();

    /**
     * 保護用戶對象
     *
     * @param user
     * @return 如果保存成功,返回true,否則,返回false
     */
    public boolean save(User user) {
        // id 從 1 開始
        Integer id = idGenerator.incrementAndGet();
        // 設置主鍵
        user.setId(id);

        return repository.put(id, user) == null;
    }

    /**
     * 返回所有用戶列表
     *
     * @return
     */
    public Collection<User> findAll(){
        return  repository.values();
    }

}

(5)建立控制層
package com.lwc.controller;

import com.lwc.pojo.User;
import com.lwc.repository.UserRespository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author eddie.lee
 * @Package com.lwc.controller
 * @ClassName UserController
 * @description
 * @date created in 2018-06-07 21:44
 * @modified by
 */
@RestController
public class UserController {

    private final UserRespository userRespository;

    @Autowired
    public UserController(UserRespository userRespository) {
        this.userRespository = userRespository;
    }

    @PostMapping("/person/save")
    public User save(@RequestParam("name") String name) {
        User user = new User();
        user.setName(name);
        if (userRespository.save(user)) {
            System.out.printf("用戶對象: %s 保存成功! ", user);
            System.out.println(" ");
        }
        return user;
    }

}

 

(6)PostMan測試,插入介面。

http://localhost:8080/person/save?name=zhangsan2

 

{
     "id": 1,
     "name": "zhangsan2"
}

 

(7)使用 Web Flux , Spring  5.0 後提出,優點是NIO
package com.lwc.config;

import com.lwc.pojo.User;
import com.lwc.repository.UserRespository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Collection;

/**
 * @author eddie.lee
 * @Package com.lwc.config
 * @ClassName RouterFunctionConfiguration
 * @description 路由器函數配置
 * @date created in 2018-06-07 22:33
 * @modified by
 */
@Configuration
public class RouterFunctionConfiguration {

//    @Autowired
//    private UserRespository userRepository;

    @Bean
    @Autowired
    public RouterFunction<ServerResponse> personFindAll(UserRespository userRepository) {
        return RouterFunctions.route(RequestPredicates.GET("/person/find/all"),
                request -> {
                    Collection<User> users = userRepository.findAll();
                    Flux<User> userFlux = Flux.fromIterable(users);
                    Mono<ServerResponse> body = ServerResponse.ok().body(userFlux, User.class);
                    return body;
                });
    }

}

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • python從2.6開始支持format,新的更加容易讀懂的字元串格式化方法, 從原來的% 模式變成新的可讀性更強的 綜合舉例說明: 輸入: '{:>18,.2f}'.format(70305084.0) # :冒號+空白填充+右對齊+固定寬度18+浮點精度.2+浮點數聲明f 輸出:' 70,305 ...
  • 原創 三羊獻瑞 觀察下麵的加法算式: 祥 瑞 生 輝 + 三 羊 獻 瑞 三 羊 生 瑞 氣 (如果有對齊問題,可以參看【圖1.jpg】) 其中,相同的漢字代表相同的數字,不同的漢字代表不同的數字。 請你填寫“三羊獻瑞”所代表的4位數字(答案唯一),不要填寫任何多餘內容。 分析:三羊生瑞氣這個數中三 ...
  • 13.1點擊更換圖形驗證碼 (1)front/signup.html (2)static/front/css/signup.css body { background: #f3f3f3; } .outer-box { width: 854px; background: #fff; margin: 0 ...
  • realloc 用方法 void realloc(void , n) 根據n的大小,如果n比較小,就沿用原來的記憶體地址(也就是返回的地址就是原來的地址),在原來地址的記憶體空間的最後面,加上n大小的記憶體空間;如果n比較大,系統就不會沿用原來的記憶體地址,系統有新開闢一個記憶體空間,並把原來記憶體空間里存放的 ...
  • Description 下麵的程式輸出結果是: A::Fun A::Do A::Fun C::Do 請填空: 程式代碼如下 ~~~~ include using namespace std; class A { private: int nVal; public: void Fun() { cout ...
  • 當我們瀏覽網頁的時候,經常會看到像下麵這些好看的圖片,你是否想把這些圖片保存下載下來。 我們最常規的做法就是通過滑鼠右鍵,選擇另存為。但有些圖片點擊滑鼠右鍵的時候並沒有另存為選項,或者你可以通過截圖工具截取下來,但這樣就降低圖片的清晰度,並且這樣效率很低。 那腫麽辦呢? 我們可以通過python 來 ...
  • Description 下麵程式的輸出結果是: destructor B destructor A 請完整寫出 class A。 限制條件:不得為 class A 編寫構造函數。 ~~~~ include using namespace std; class A { // Your Code Her ...
  • 一、概念 早期的 Java API 只支持由本地系統套接字型檔提供所謂的阻塞函數來支持網路編程。由於是阻塞 I/O ,要管理多個併發客戶端,需要為每個新的客戶端Socket 創建一個 Thread 。這將導致一系列的問題,第一,在任何時候都可能有大量的線程處於休眠狀態(不可能每時每刻都有對應的併發數) ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...