【SpringBoot】Web Fluxを統合

5452 ワード

前言:
SprigBoot基礎知識を身につけなければなりません.
プロフィール:
Takes an opinionatied view of building production-ready Spring aplications.Spring Boot favors convent over configration and is designed to get you up and running as quicklyas possible.
ツール:
JDK 8
apache-maven-35.2
IntelliJ IDEA 2018.1.3 x 64
 
(1)springbootプロジェクトを新規作成する
(2)pom.xml
    
        
            org.springframework.boot
            spring-boot-starter-webflux
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            io.projectreactor
            reactor-test
            test
        

        
            org.projectlombok
            lombok
        
    
 
(3)エンティティUserを確立する(簡略化して書かない)
(4)レクサスを立てる
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 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 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」:「zhhang san 2」)
 
(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 personFindAll(UserRespository userRepository) {
        return RouterFunctions.route(RequestPredicates.GET("/person/find/all"),
                request -> {
                    Collection users = userRepository.findAll();
                    Flux userFlux = Flux.fromIterable(users);
                    Mono body = ServerResponse.ok().body(userFlux, User.class);
                    return body;
                });
    }

}
【SpringBoot】Web Fluxを統合
タグ:cto   sh   ユーザー   さん   インターフェース   pid   ctr   .ベスト   mic   
原文の住所:https://www.cnblogs.com/EddieBlog/p/9157496.html