1 环境搭建

  1.1 版本说明

    jdk -> 1.8

    maven -3.5

    springboot -> 2.0.3

    开发工具 -> IDEA

  1.2 创建项目

    利用 IDEA 或者 start.spring.io 创建一个SpringBoot项目

    1.2.1 选择依赖

      

    1.2.2 pom.xml 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion> <groupId>cn.xiangxu</groupId>
<artifactId>test_demo</artifactId>
<version>0.0.-SNAPSHOT</version>
<packaging>jar</packaging> <name>test_demo</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0..RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<!--<scope>runtime</scope>-->
<optional>true</optional>
</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>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build> </project>

2 开发步骤

  2.1 处理器类

    相当于SpringMVC中的service类,主要就是根据不同的请求url执行不同的业务逻辑

    技巧01:编写的处理器类必须被容器所管理

package cn.xiangxu.test_demo.handler;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono; /**
* @author 王杨帅
* @create 2018-06-26 9:58
* @desc 相当于springMVC的controller类
**/
@Component
public class StudentHandler { public Mono<ServerResponse> helloStudent(ServerRequest serverRequest) {
return ServerResponse // 响应对象封装
.ok() // 响应码
.contentType(MediaType.TEXT_PLAIN) // 响应类型
.body(BodyInserters.fromObject("这是学生控制类")); // 响应体
} }

StudentHandler.java

  2.2 路由器类

    相当于DispatherServlet,主要就是将不同的请求url和对应的处理器类进行匹配

    技巧01:路由器类相当于一个Java配置类,所以必须添加 @Configuration 注解,并且路由器类中的路由方法返回的对象必须被容器管理,所以必须添加@Bean注解

    技巧02:RouterFunctions的route方法接收两个参数,并返回 RouterFunctions 类型

      》参数一 :请求断言

      》参数二 :处理函数接口【可用lambda表达式代替】

package cn.xiangxu.test_demo.router;

import cn.xiangxu.test_demo.handler.StudentHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
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; /**
* @author 王杨帅
* @create 2018-06-26 10:02
* @desc
**/
@Configuration
public class StudentRouter {
@Bean
public RouterFunction<ServerResponse> routerHelloStudent(StudentHandler studentHandler) {
return RouterFunctions
.route( // 路由匹配:将请求url路由到对应的处理器
RequestPredicates
.GET("/student/hello") // 请求url
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), // 接收的请求数据类型
studentHandler::helloStudent // 处理请求的处理方法
);
/*
代码解释:如果请求是/student/hello时就会调用StudentHandler处理器类中的helloStudent方法进行业务逻辑处理
*/
}
}

StudentRouter.java

  2.3 启动项目

    2.3.1 IDEA启动

      直接运行启动类就可以啦

    2.3.2 打包部署启动

      》打包项目

      

      》运行项目

        进入到项目根目录下的target文件夹后通过 java -jar xxx.jar 就可以运行项目啦

        技巧01:SpringBoot WebFlux 默认集成的是 Netty

        

    2.3.3 前端访问

      

WebFlux02 SpringBoot WebFlux项目骨架搭建的更多相关文章

  1. Maven项目骨架搭建

    1. 如何使用Maven的archetype快速生成一个新项目 2. Maven之自定义archetype生成项目骨架(一) 3. 使用maven3 创建自定义的archetype 4. 使用mave ...

  2. SpringBoot→Maven项目快速搭建

    使用软件 :SpringToolSuite4 打开软件后在Package Explorer 栏中点击右键 Spring starter project 等待反应 填写完毕之后点击next     fi ...

  3. springboot web项目创建及自动配置分析(thymeleaf+flyway)

    @ 目录 源码分析 webjars thymeleaf thymeleaf语法 springmvc 启动配置原理 集成flyway插件 springboot 创建web项目只需要引入对应的web-st ...

  4. 快速搭建react项目骨架(按需加载、redux、axios、项目级目录等等)

    一.前言 最近整理了一下项目骨架,顺便自定义了一个脚手架,方便日后使用.我会从头开始,步骤一步步写明白,如果还有不清楚的可以评论区留言.先大致介绍一下这个骨架,我们采用 create-react-ap ...

  5. 手把手教你从零开始搭建SpringBoot后端项目框架

    原料 新鲜的IntelliJ IDEA.一双手.以及电脑一台. 搭建框架 新建项目 打开IDE,点击File -> New Project.在左侧的列表中的选择Maven项目,点击Next. 填 ...

  6. SpringBoot学习(一)—— web项目基础搭建

    首先我们在浏览器打开这个网站 https://start.spring.io/ 打开后可以看到以下页面 在这里我们可以快速搭建一个SpringBoot基础项目,填写和选择完相应的信息后,我们点击那个绿 ...

  7. eclipse搭建springboot的项目

    记录一次自己搭建springboot的经历 springboot项目创建 这里借用别的博主分享的方法 https://blog.csdn.net/mousede/article/details/812 ...

  8. WebFlux03 SpringBoot WebFlux实现CRUD

    1 准备 基于SpringBoot2.0搭建WebFlux项目,详情请参见三少另外一篇博文 2 工程结构 <?xml version="1.0" encoding=" ...

  9. Angular企业级开发(5)-项目框架搭建

    1.AngularJS Seed项目目录结构 AngularJS官方网站提供了一个angular-phonecat项目,另外一个就是Angular-Seed项目.所以大多数团队会基于Angular-S ...

随机推荐

  1. 【vs2013】如何在VS的MFC中配置使用GDI+?

    摘自:http://www.cnblogs.com/CSGrandeur/p/3156843.html (已实验,可行) 1.配置GDI+ VS2010自带GDI+,直接使用. (1)首先要添加头文件 ...

  2. JFinal自定义FreeMarker标签

    为什么采用freemarker? 1.模板技术,不依附于语言和框架,前端和后端解耦,便于分工协作,更好的协同. 2.页面相应速度快 3.前端非常的灵活,采用自定义标签可以在不更改后端的基础上很容易的构 ...

  3. 关于Bootstrap table的回调onLoadSuccess()和onPostBody()使用小结

    关于Bootstrap table的回调onLoadSuccess()和onPostBody()使用小结 Bootstrap table 是一款基于 Bootstrap 的 jQuery 表格插件, ...

  4. 动态可缓存的内容管理系统(CMS)

    摘要:内容管理系统(CMS)在各大商业站点和门户站点中扮演着重要的角色,是内容有效组织和快速发布极为重要的基础平台.目前主流的内容发布系统都使用静态页面进行内容发布,在我们的实际使用过程中我们深切的感 ...

  5. hdu 3625 Examining the Rooms——第一类斯特林数

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3625 n^2 求斯特林数就行.要减去的就是1号钥匙在1号房间的方案,即 s[ n-1 ][ m-1] . ...

  6. SQL Server数据库优化经验总结

    优化数据库的注意事项: 1.关键字段建立索引. 2.使用存储过程,它使SQL变得更加灵活和高效. 3.备份数据库和清除垃圾数据. 4.SQL语句语法的优化.(可以用Sybase的SQL Expert, ...

  7. Spring读取properties资源文件

    我们知道可以通过读取资源文件流后加载到Properties对象,再使用该对象方法来获取资源文件.现在介绍下利用Spring内置对象来读取资源文件. 系统启动时加载资源文件链路:web.xml --&g ...

  8. 第12篇 PSR-1规范

    这个规范也不多,七点如下: 1. Overview Files MUST use only <?php and <?= tags. Files MUST use only UTF-8 wi ...

  9. mysql中修改密码的方式

    参考地址:https://www.cnblogs.com/yang82/p/7794712.html mysql中修改用户密码的方式: 最简单的方法就是借助第三方工具Navicat for MySQL ...

  10. [三卷天书]记一个asp.net生成两个日期范围内生成随机时间的方法

    想网上找个生成随机天数的方法找不到,后面只得自己写了,贴给大家方便使用 思路:算两个日期的相差天数,然后在0到相差天数的范围内生成随机数,再用结束时间的天数部分减去这个随机数,代码: /// < ...