前言:

必需学会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. win2003 HookPort 服务启动失败的解决办法!

    Win2003系统每次开机启动时都弹出个对话框报HookPort 服务启动失败,很多网友都遇到同类问题,问题根源是360安全卫士引起的,官方一直没有给出解决方案 问题描述:Win2003系统每次开机启 ...

  2. pymongo的安装和使用

    1.安装 MongoDB的python接口pymongo的安装方法有多种,如源码.easy_install.pip都可以.采用pip安装,很简单. pip install pymongo 安装完成后可 ...

  3. Linux下Java性能监控

    Linux下Java性能监控 一.JVM堆内存使用监控 获取thread dump的3种方法: 1)使用$JAVA_HOME/bin/jcosole中的MBean,到MBean>com.sun. ...

  4. javaSE web开发 登录思路代码

    准备工作: 1.在WebContent  WEB-INF   lib下添加c3p0-0.9.5.2.jar     mchange-common-java-0.2.11.jar   ojdbc6.ja ...

  5. docker搭建本地私仓

    环境centos7  docker-ce 18 启动仓库镜像 docker run -d -p 5000:5000 registry:2 docker images 通过docker tag 标识镜像 ...

  6. 【原创】如何设置Virtual Box虚拟机CentOS7为静态IP地址

    如何设置Virtual Box虚拟机CentOS7为静态IP地址 最近要搭建一个Kubernetes集群,需要设置虚拟机为静态IP地址不变.翻了一些资料,参差不齐,有些也比较过时了.自己实测总结了一下 ...

  7. JAVA中的一些内置方法

    Math 函数: Math.E //自然常数e Math.abs(12.3); //返回该值的绝对值 Math.ceil(12.3); //向上取整 Math.floor(12.3); //向下取整 ...

  8. http协议的总结

    HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统,其主要特点概括如下: 1.支持客户/服务器模式. 2.简单快速:客户向服务器请求服务时,只需传送请求方法和 ...

  9. js 实现内容的展开和收缩

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  10. Redis(三)

    1.服务器部署好redis后 在项目里边添加 StackExchange.Redis.dll (可以去nuget下载) 2.添加SERedisHelper.cs  里边有  对应的缓存的相关方法 us ...