"Springboot" Integrated Web Flux

Source: Internet
Author: User

Objective:

Must learn to springboot basic knowledge

Brief introduction:

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

Tools:

JDK8

apache-maven-3.5.2

IntelliJ Idea 2018.1.3 x64

(1) Create a new Springboot project (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&lt            ;/artifactid> <scope>test</scope> </dependency> <dependency>            <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupid>org.projec tlombok</groupid> <artifactId>lombok</artifactId> </dependency> </dependen Cies>
(3) Establishment of entity User (simplified not written) (4) Establishment of 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 was DAO * @date created in 2018-06-07 21:36 * @modified by */@Repositorypublic class use rrespository {/** * use memory to store = = = * Map */private Concurrentmap<integer, user> repository = new Concur    Renthashmap<> ();    /** * ID Generation */private final static Atomicinteger Idgenerator = new Atomicinteger ();         /** * Protect User Object * * @param user * @return If the Save succeeds, returns True, otherwise, returns false */public boolean save (user user) {        ID starting from 1 Integer id = idgenerator.incrementandget ();        Set the primary key User.setid (ID);    return Repository.put (ID, user) = = NULL; }/** * returnBack to all Users list * * @return */public collection<user> FindAll () {return repository.values (); }}
(5) Establishing the control layer
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 * * @RestControllerpublic 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) {System.out.printf ("User object:%s saved successfully!            ", user);        System.out.println ("");     }   return user; }}

(6) Postman test, insert interface.

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

{
"id": 1,
"Name": "Zhangsan2"
}

(7) using Web Flux, Spring 5.0, the advantage is 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 Router function configuration * @date created in 2018-06-07 22:33 * @modified by */@Configurationpubl    IC class Routerfunctionconfiguration {//@Autowired//private userrespository userrepository; @Bean @Autowired Public routerfunction<serverresponse&Gt Personfindall (Userrespository userrepository) {return Routerfunctions.route (Requestpredicates.get ("/person/find/a                    ll "), request, {collection<user> users = Userrepository.findall ();                    flux<user> Userflux = flux.fromiterable (users);                    Mono<serverresponse> BODY = Serverresponse.ok (). Body (Userflux, user.class);                return body;    }); }}

"Springboot" Integrated Web Flux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.