Flowable实战(二)集成Springboot
1、创建Springboot项目
打开IDEA,通过File -> New -> Project… -> Spring Initializr 创建一个新的Springboot项目

在下一个界面,填入项目名Name,JDK选择8

接着,选择Springboot 2.6.2

点击完成

生成空的Springboot项目,pom.xml文件内容:

2、加入Flowable依赖包
修改pom.xml文件
"properties"属性下加入:
<flowable.version>6.7.2</flowable.version>
注意,请确保Flowable版本与Springboot版本匹配,否则会无法启动。查看Flowable不同版本对应的springboot版本,参考:https://blog.csdn.net/JinYJ2014/article/details/121530632
"dependencies"属性下加入:
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter</artifactId>
<version>${flowable.version}</version>
</dependency>
这个依赖会自动向classpath添加正确的Flowable与Spring依赖。
注意:有时候,依赖JAR无法自动获取,可以右键点击项目,并选择 Maven ->Reload Project以强制手动刷新。
现在可以编写Spring Boot应用了:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FlowableExampleApplication {
public static void main(String[] args) {
SpringApplication.run(FlowableExampleApplication.class, args);
}
}
Flowable需要数据库来存储数据。运行上面的代码会得到异常提示,指出需要在classpath中添加数据库驱动依赖。
3、添加数据源
现在添加MySQL数据库依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
注意:MySQL依赖包版本根据自己所连接的数据库版本修改,否则可能会连接失败
application.properties文件中添加数据源
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/flowable?characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.username=jinyangjie
spring.datasource.password=jinyangjie
应用成功启动后,查看数据库,可以看到,已经自动创建了Flowable表:

注意:如果出现“Caused by: java.lang.RuntimeException: Exception while initializing Database connection”的错误,请确保数据源的配置项正确,并检查MySQL依赖包版本是否匹配
4、REST支持
4.1 添加REST依赖
通常我们的应用会使用REST API。添加下列依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<spring.boot.version>2.6.2</spring.boot.version>
下面做个Controller和Service层的简单使用示例,例子来源于Flowable官方文档。
4.2 添加流程文件
resources/processes目录下的任何BPMN 2.0流程定义都会被自动部署。创建processes目录,并在其中创建示例流程定义(命名为one-task-process.bpmn20.xml)。
<?xml version="1.0" encoding="UTF-8"?>
<definitions
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:flowable="http://flowable.org/bpmn"
targetNamespace="Examples">
<process id="oneTaskProcess" name="The One Task Process">
<startEvent id="theStart" />
<sequenceFlow id="flow1" sourceRef="theStart" targetRef="theTask" />
<userTask id="theTask" name="my task" flowable:assignee="jinyangjie" />
<sequenceFlow id="flow2" sourceRef="theTask" targetRef="theEnd" />
<endEvent id="theEnd" />
</process>
</definitions>
4.3 serivice层代码示例
创建一个新的Spring服务类,并创建两个方法:一个用于启动流程,另一个用于获得给定任务办理人的任务列表。在这里只是简单地包装了Flowable调用,在实际使用场景中会比这复杂得多。
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class MyService {
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Transactional
public void startProcess() {
runtimeService.startProcessInstanceByKey("oneTaskProcess");
}
@Transactional
public List<Task> getTasks(String assignee) {
return taskService.createTaskQuery().taskAssignee(assignee).list();
}
}
4.4 controller层代码示例
@RestController
public class MyRestController {
@Autowired
private MyService myService;
@RequestMapping(value="/process", method= RequestMethod.POST)
public void startProcessInstance() {
myService.startProcess();
}
@RequestMapping(value="/tasks", method= RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public List<TaskRepresentation> getTasks(@RequestParam String assignee) {
List<Task> tasks = myService.getTasks(assignee);
List<TaskRepresentation> dtos = new ArrayList<TaskRepresentation>();
for (Task task : tasks) {
dtos.add(new TaskRepresentation(task.getId(), task.getName()));
}
return dtos;
}
static class TaskRepresentation {
private String id;
private String name;
public TaskRepresentation(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
Spring Boot会自动扫描组件,并找到我们添加在应用类上的@Service与@RestController。再次运行应用,现在可以与REST API交互了。例如使用cURL:
curl http://localhost:8080/tasks?assignee=jinyangjie
[]
curl -X POST http://localhost:8080/process
curl http://localhost:8080/tasks?assignee=jinyangjie
[{"id":"b6350a6d-7070-11ec-bd1b-0a0027000006","name":"my task"}]
5、小结
本篇介绍了Springboot的初步集成,很明显还有很多Spring Boot相关的内容还没有提及,比如打包WAR文件、Spring Security支持等,这些将在后面的章节中介绍。
Flowable实战(二)集成Springboot的更多相关文章
- 实战:docker搭建FastDFS文件系统并集成SpringBoot
实战:docker搭建FastDFS文件系统并集成SpringBoot 前言 15年的时候,那时候云存储还远远没有现在使用的这么广泛,归根结底就是成本和安全问题,记得那时候我待的公司是做建站开发的,前 ...
- SpringSecurity权限管理系统实战—二、日志、接口文档等实现
系列目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战 ...
- Flowable实战(八)BPMN2.0 任务
任务是流程中最重要的组成部分.Flowable提供了多种任务类型,以满足实际需求. 常用任务类型有: 用户任务 Java Service任务 脚本任务 业务规则任务 执行监听器 任务监听器 多 ...
- coreseek实战(二):windows下mysql数据源部分配置说明
coreseek实战(二):windows下mysql数据源部分配置说明 关于coreseek在windows使用mysql数据源的配置,以及中文分词的详细说明,请参考官方文档: mysql数据源配置 ...
- 【NFS项目实战二】NFS共享数据的时时同步推送备份
[NFS项目实战二]NFS共享数据的时时同步推送备份 标签(空格分隔): Linux服务搭建-陈思齐 ---本教学笔记是本人学习和工作生涯中的摘记整理而成,此为初稿(尚有诸多不完善之处),为原创作品, ...
- chrome调试工具高级不完整使用指南(实战二)
3.3 给页面添加测试脚本 在现实的工作中,我们往往会遇到一些问题在线上就会触发然后本地就触发不了的问题.或者是,要给某个元素写一个测试脚本.这个时候如果是浏览器有提供一个添加脚本的功能的话,那么我们 ...
- Spring Maven项目集成Springboot
Maven管理的Spring项目,准备集成Springboot做接口 1.Springboot对Spring有版本要求 我用的Springboot版本:1.4.5.RELEASE,对应Spring的版 ...
- Python爬虫实战二之爬取百度贴吧帖子
大家好,上次我们实验了爬取了糗事百科的段子,那么这次我们来尝试一下爬取百度贴吧的帖子.与上一篇不同的是,这次我们需要用到文件的相关操作. 前言 亲爱的们,教程比较旧了,百度贴吧页面可能改版,可能代码不 ...
- 转 Python爬虫实战二之爬取百度贴吧帖子
静觅 » Python爬虫实战二之爬取百度贴吧帖子 大家好,上次我们实验了爬取了糗事百科的段子,那么这次我们来尝试一下爬取百度贴吧的帖子.与上一篇不同的是,这次我们需要用到文件的相关操作. 本篇目标 ...
- Netty 仿QQ聊天室 (实战二)
Netty 聊天器(百万级流量实战二):仿QQ客户端 疯狂创客圈 Java 分布式聊天室[ 亿级流量]实战系列之15 [博客园 总入口 ] 源码IDEA工程获取链接:Java 聊天室 实战 源码 写在 ...
随机推荐
- [BUUCTF]PWN——hitcontraining_heapcreator
hitcontraining_heapcreator 附件 步骤: 例行检查,64位程序,开启了canary和nx 本地试运行一下,看看大概的情况,经典的堆的菜单 64位ida载入,main函数没有什 ...
- Linux中find命令与三剑客之grep和正则
昨日内容回顾 1.每个月的3号.5号和15号,且这天时周六时 执行 00 00 3,5,15 * 6 2.每天的3点到15点,每隔3分钟执行一次 */3 3-15 * * * 3.每周六早上2点半执行 ...
- CF999A Mishka and Contest 题解
Content 能力值为 \(k\) 的小 M 参加一次考试,考试一共有 \(n\) 道题目,每道题目的难度为 \(a_i\).小 M 会选择两头中的一道难度不超过他的能力值题目去做,每做完一道,这道 ...
- LuoguP7369 [COCI2018-2019#4] Elder 题解
Content 有一个魔杖最初在 \(Z\) 巫师中.经过 \(n\) 轮较量,第 \(i\) 轮中,\(Z_{i,1}\) 巫师打败了 \(Z_{i,2}\) 巫师.如果一个巫师打败了拥有魔杖的巫师 ...
- Hystrix 监控可视化页面——Dashboard 流监控
1.什么是Dashboard Hystrix-dashboard 是一款针对 Hystrix 进行实时监控的工具页面,通过 Hystrix Dashboard 我们可以在直观地看到各 Hystrix ...
- 当ligerui的grid出现固定列与非固定列不在同一水平线上时,改怎么处理
当ligerui的grid出现固定列与非固定列不在同一水平线上时,如下图所示: 此时可以增加fixedCellHeight:false属性进行解决.这个属性在IE上不起作用,那么该怎么处理,可以这样处 ...
- Json解析案例-teachers数据集
背景: 通过平台执行接口时,接口往往返回的JSON串,所以平台要能提供方便快捷的JSON解析函数. 一.Json字符串: 1 { 2 "lemon": { 3 "teac ...
- IDEA结合mybatis插件自动生成代码
pom文件 添加插件 <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>myb ...
- 魔法串(hud4545)
魔法串 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submiss ...
- Python 英语单词本
python pymysql re requests socket库的简单运用 要考试了,这里用所学的知识做一个实例 pymysql库 这个库是用来连接数据库的,使用数据库语句在python里创建表和 ...