前言:

必需学会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的更多相关文章

  1. SpringBoot 集成Web

    1,静态资源访问: 在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置 Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则: ...

  2. springBoot集成web service

    转载大神: https://blog.csdn.net/u011410529/article/details/68063541?winzoom=1 https://blog.csdn.net/nr00 ...

  3. Shiro集成web环境[Springboot]-认证与授权

    Shiro集成web环境[Springboot]--认证与授权 在登录页面提交登陆数据后,发起请求也被ShiroFilter拦截,状态码为302 <form action="${pag ...

  4. Shiro集成web环境[Springboot]-基础使用

    Shiro集成web环境[Springboot] 1.shiro官网查找依赖的jar,其中shiro-ehcache做授权缓存时使用,另外还需要导入ehcache的jar包 <dependenc ...

  5. Springboot搭建web项目

    最近因为项目需要接触了springboot,然后被其快速零配置的特点惊呆了.关于springboot相关的介绍我就不赘述了,大家自行百度google. 一.pom配置 首先,建立一个maven项目,修 ...

  6. springboot集成Actuator

    Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...

  7. SpringBoot集成jsp

    一.springBoot集成jsp: 1.修改pom文件 <!--集成jsp所需jar包--> <!--jsp页面使用jstl标签--> <dependency> ...

  8. springboot集成schedule(深度理解)

    背景 在项目开发过程中,我们经常需要执行具有周期性的任务.通过定时任务可以很好的帮助我们实现. 我们拿常用的几种定时任务框架做一个比较: 从以上表格可以看出,Spring Schedule框架功能完善 ...

  9. Windows环境下springboot集成redis的安装与使用

    一,redis安装 首先我们需要下载Windows版本的redis压缩包地址如下: https://github.com/MicrosoftArchive/redis/releases 连接打开后如下 ...

随机推荐

  1. PAT——1038. 统计同成绩学生

    本题要求读入N名学生的成绩,将获得某一给定分数的学生人数输出. 输入格式: 输入在第1行给出不超过105的正整数N,即学生总人数.随后1行给出N名学生的百分制整数成绩,中间以空格分隔.最后1行给出要查 ...

  2. 404 Note Found 队 Alpha 6

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  3. 框架 Hibernate

    Hibernate 在test01右键新建其他找到hibernate文件夹下的Hibernate Configuration File(cfg.xml) <?xml version=" ...

  4. IOS 创建简单表视图

    创建简单表视图 此实例主要实现UITableViewDataSource协议中必需要实现的两个方法tableView:numberOfRowsInSection: 和tableView:cellFor ...

  5. NIO流—理解Buffer、Channel概念和NIO的读写操作

    NIO流与IO流的区别 面向流与面向块 IO流是每次处理一个或多个字节,效率很慢(字符流处理的也是字节,只是对字节进行编码和解码处理). NIO流是以数据块为单位来处理,缓冲区就是用于读写的数据块.缓 ...

  6. 常用PHP方法

    个人常用的一些方法记录/** * 返回错误 * * @param int $err_no * @param string $err_msg * @param array $data * @return ...

  7. HCNA(一)网络传输介质

    一 .同轴线缆 介绍:同轴线缆是一种早期的网络传输介质,同轴电缆的得名与它的结构相关,由内导体.外导体.绝缘介质和防护套四部分组成.同样支持10Mbps传输速率.现在已经基本被淘汰,不在应用于企业网络 ...

  8. md5.digest()与md5.hexdigest()之间的区别及转换

    举给例子 md5 = hashlib.md5('adsf') md5.digest() //返回: '\x05\xc1*(s48l\x94\x13\x1a\xb8\xaa\x00\xd0\x8a' # ...

  9. pycharm社区版新建django文件不友好操作

    一.cmd操作 1.django-admin startproject (新建project名称) 2.在pycharm打开project,运行终端输入:python manage.py starta ...

  10. [Golang学习笔记] 04 程序实体1 变量声明

    变量声明: Go语言的程序实体包含:变量.常量.函数.结构体和接口,是一门静态类型的编程语言. (在声明变量或常量的时候,需要指定类型,或者给予足够信息是的Go语言能够推导出类型) Go语言变量的类型 ...