1. 程式人生 > >【SpringBoot】集成 Web Flux

【SpringBoot】集成 Web Flux

ssi ren 接口 you ini TP AS AD util

前言:

必需學會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;
                });
    }

}

【SpringBoot】集成 Web Flux