springboot项目集成activiti-modeler工作流,页面设计器部署到项目中

本文主要是介绍springboot项目集成activiti-modeler工作流,页面设计器部署到项目中,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

接着上篇文章idea插件画流程图,这种方法在项目中会使用但是用起来感觉不方便,需要将吧bpmn、png文件保存在一个固定的路径下,部署此文件。

而本篇文章不需要,只需要在项目页面中画好流程,直接部署流程。具体操作需要用到

activiti源码,改动部分代码即可。这里是5.22.0版本,下载好的在下方链接直接下载就好。

https://pan.baidu.com/s/1MU_uALvU_nB1piNq3ihrJA?pwd=bovw

1、将Activiti-activiti-5.22.0\modules\activiti-webapp-explorer2\src\main\webapp下的三个文件放到resources的static中

2、将Activiti-activiti-5.22.0\modules\activiti-webapp-explorer2\src\main\resources的json文件放到resources的static中

3、Activiti-activiti-5.22.0\modules\activiti-modeler\src\main\java\org\activiti\rest\editor这两个文件夹下的java文件放到com.atxinxin.activiti下

 

 

整体目录结构(包名自己定义就好)

 完成以上步骤,需要修改ModelEditorJsoRestResource.java、ModelSaveResource.java、StencilsetRestResource.java

添加@RequestMapping("/activiti-explorer/service")

例:

修改 StencilsetRestResource.java文件

@RestController
@RequestMapping("/activiti-explorer/service")
public class StencilsetRestResource {@RequestMapping(value="/editor/stencilset", method = RequestMethod.GET, produces = "application/json;charset=utf-8")public @ResponseBody String getStencilset() {InputStream stencilsetStream = this.getClass().getClassLoader().getResourceAsStream("static/stencilset.json");try {return IOUtils.toString(stencilsetStream, "utf-8");} catch (Exception e) {throw new ActivitiException("Error while loading stencil set", e);}}
}

在此所有文件修改完成。

 接下来配置端口、数据源、等等

--- 先来activiti的数据源配置文件

/*** @author weixinxin* @Description actityti数据源配置* @Date 11:22 2023/07/06*/
@Configuration
public class ActitytiDataSourceConfig extends AbstractProcessEngineAutoConfiguration {@Bean(name = "activitiDataSource")@ConfigurationProperties(prefix = "spring.datasource")public DataSource activitiDataSource(){System.out.println("-------------------------");return new DruidDataSource();}@Beanpublic PlatformTransactionManager transactionManager() {return new DataSourceTransactionManager(activitiDataSource());}@Beanpublic SpringProcessEngineConfiguration springProcessEngineConfiguration() {SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();configuration.setDataSource(activitiDataSource());configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);configuration.setJobExecutorActivate(true);configuration.setTransactionManager(transactionManager());return configuration;}}

--- application-dev.properties配置文件

server.port=9999
spring.datasource.validationQuery=select 1 from dual
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf-8&useSSL=false&&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
spring.datasource.password = 123456
spring.datasource.username= root# &nullCatalogMeansCurrent=true?????mybatis.mapper-locations=classpath:com/atxinxin/mapper/*.xml  #mybatis??
spring.activiti.check-process-definitions = false
#spring.activiti.database-schema-update=false
logging.file.name=log.log
logging.pattern.level=debugsecurity.basic.enabled=false

--- 启动类

/*** @author weixinxin* @Description 启动类* @Date 11:26 2023/07/06*/
@Slf4j
@SpringBootApplication(exclude = {org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class, org.activiti.spring.boot.SecurityAutoConfiguration.class})
public class ActivitiDemlApplication {public static void main(String[] args) {SpringApplication.run(ActivitiDemlApplication.class, args);log.info("---- 启动成功 !! ----------");}}

以上就是所有的准备工作

接下来需要写测试类进行测试。

1、先创建模型,跳转到流程编辑页面

2、部署流程

3、启动流程

4、提交任务(完成任务)


/*** @author weixinxin_ext 2023-07-06**/
@Controller
@Slf4j
@RequestMapping("/activiti-explorer/service")
public class MyActivitiController {@Resourceprivate RepositoryService repositoryService;/*** @Description 创建模型* @author weixinxin* @Date 18:12 2023/6/30**/@RequestMapping(value = "/create")public void create(HttpServletRequest request, HttpServletResponse response) throws IOException {String id = "";String name = "出差申请-Travel approval process";String description = "出差申请-Travel approval process";String key = "travel_approval_process";ObjectMapper objectMapper = new ObjectMapper();ObjectNode editorNode = objectMapper.createObjectNode();editorNode.put("id", "canvas");editorNode.put("resourceId", "canvas");ObjectNode stencilSetNode = objectMapper.createObjectNode();stencilSetNode.put("namespace", "http://b3mn.org/stencilset/bpmn2.0#");editorNode.put("stencilset", stencilSetNode);Model modelData = this.repositoryService.newModel();ObjectNode modelObjectNode = objectMapper.createObjectNode();modelObjectNode.put("name", name);modelObjectNode.put("revision", 1);description = StringUtils.defaultString(description);modelObjectNode.put("description", description);modelData.setMetaInfo(modelObjectNode.toString());modelData.setName(name);modelData.setKey(StringUtils.defaultString(key));this.repositoryService.saveModel(modelData);this.repositoryService.addModelEditorSource(modelData.getId(), editorNode.toString().getBytes("utf-8"));id = modelData.getId();//一定要输出id,部署需要,刚接触activiti不知道这个id在哪里System.out.println("id ========= " + id);response.sendRedirect("/modeler.html?modelId=" + id);}/*** @param id * @Description 发布模型为流程定义* @Date 14:04 2023/07/06* @author weixinxin*/@RequestMapping("/deploy/{id}")@ResponseBodypublic void deploy(@PathVariable("id") String id) throws Exception {Model modelData = repositoryService.getModel(id);byte[] bytes = repositoryService.getModelEditorSource(modelData.getId());JsonNode modelNode = new ObjectMapper().readTree(bytes);BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);if (model.getProcesses().size() == 0) {log.error("数据模型不符要求,请至少设计一条主线流程。");}byte[] bpmnBytes = new BpmnXMLConverter().convertToXML(model);String processName = modelData.getName() + ".bpmn20.xml";repositoryService.createDeployment().name(modelData.getName()).addString(processName, new String(bpmnBytes, "UTF-8")).deploy();}/*** @param keyName * @Description 启动流程* @Date 14:03 2023/07/06* @author weixinxin*/@RequestMapping("/start/{key}")@ResponseBodypublic Object startProcess(@PathVariable("key") String keyName) {ProcessInstance process = processEngine.getRuntimeService().startProcessInstanceByKey(keyName);return process.getId() + " : " + process.getProcessDefinitionId();}/*** @param processInstanceId 流程实例id* @Description 提交任务* @Date 14:03 2023/07/06* @author weixinxin*/@RequestMapping("/run/{processInstanceId}")@ResponseBodypublic Object run(@PathVariable("processInstanceId") String processInstanceId) {Task task = processEngine.getTaskService().createTaskQuery().processInstanceId(processInstanceId).singleResult();System.out.println("task {} find " + task.getId());processEngine.getTaskService().complete(task.getId());return "SUCCESS";}
}

 创建模型,流程编辑页面

以上完成测试类编写,开始测试

1、创建模型,跳转流程设计页面

http://localhost:9999/activiti-explorer/service/create

2、部署流程(id换成自己的)

http://localhost:9999/activiti-explorer/service/deploy/id

3、启动流程(key换成自己的)

http://localhost:9999/activiti-explorer/service/start/key

4、完成任务(流程实例id换成自己的)
http://localhost:9999/activiti-explorer/service/run/processInstanceId
 

 1、当创建模型,跳转流程设计页面时,创建完成流程图点击保存会发现返回的状态码为400

http://localhost:9999/activiti-explorer/service/create

在看一下返回的信息

在ModelSaveResource.java中

后端接受参数,可以看到需要(MultiValueMap<String,String> values)

@RequestMapping(value = "/model/{modelId}/save", method = RequestMethod.PUT)@ResponseStatus(value = HttpStatus.OK)public void saveModel(@PathVariable String modelId, @RequestBody MultiValueMap<String, String> values)

 前端传参(删除了许多,为了方便看需要哪些参数)可以看到(是 json_xml,svg_xml,name,descriotin)

json_xml: {"resourceId":"5007","properties":{}
svg_xml: <svg xmlns="http://www.w3.org/2000/svg" </svg>
name: 请假申请-Leave approval process
description: 请假申请-Leave approval process

这就需要我们来修改ModelSaveResource.java中的saveModel方法

/*** @Description 保存模型* @Date 15:52 2023/07/07* @author weixinxin*/
@RequestMapping(value = "/model/{modelId}/save", method = RequestMethod.PUT)@ResponseStatus(value = HttpStatus.OK)public void saveModel(@PathVariable String modelId,@RequestParam("json_xml") String json_xml,@RequestParam("svg_xml") String svg_xml,@RequestParam("name") String name,@RequestParam("description") String description) {try {Model model = repositoryService.getModel(modelId);ObjectNode modelJson = (ObjectNode) objectMapper.readTree(model.getMetaInfo());modelJson.put(MODEL_NAME, name);modelJson.put(MODEL_DESCRIPTION, description);model.setMetaInfo(modelJson.toString());model.setName(name);repositoryService.saveModel(model);repositoryService.addModelEditorSource(model.getId(), json_xml.getBytes("utf-8"));InputStream svgStream = new ByteArrayInputStream(svg_xml.getBytes("utf-8"));TranscoderInput input = new TranscoderInput(svgStream);PNGTranscoder transcoder = new PNGTranscoder();// Setup outputByteArrayOutputStream outStream = new ByteArrayOutputStream();TranscoderOutput output = new TranscoderOutput(outStream);// Do the transformationtranscoder.transcode(input, output);final byte[] result = outStream.toByteArray();repositoryService.addModelEditorSourceExtra(model.getId(), result);outStream.close();} catch (Exception e) {LOGGER.error("Error saving model", e);throw new ActivitiException("Error saving model", e);}}

按理说这是activiti的源码,传参和接受参数应该一样,但是为什么不一样需要找到前端的方法看一下,目前因为时间问题,没时间看,到时候有时间,找到原因我在补充吧!

看了一下原因,如果不想修改后端接受收参数,那就修改一下请求方式,

后端 :put 修改为 post

@RequestMapping(value = "/model/{modelId}/save", method = RequestMethod.POST)

前端:static/editor-app/configuration/toolbar-default-actions.js文件中

$scope.save = function (successCallback)

这个方法的请求方式修改为post

$http({    method: 'POST',data: params,ignoreErrors: true,headers: {'Accept': 'application/json','Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},transformRequest: function (obj) {var str = [];for (var p in obj) {str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));}return str.join("&");},url: KISBPM.URL.putModel(modelMetaData.modelId)})

这样就不需要修改接收参数就可以保存流程模型了

这篇关于springboot项目集成activiti-modeler工作流,页面设计器部署到项目中的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/590000

相关文章

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

Java中Redisson 的原理深度解析

《Java中Redisson的原理深度解析》Redisson是一个高性能的Redis客户端,它通过将Redis数据结构映射为Java对象和分布式对象,实现了在Java应用中方便地使用Redis,本文... 目录前言一、核心设计理念二、核心架构与通信层1. 基于 Netty 的异步非阻塞通信2. 编解码器三、

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

一篇文章彻底搞懂macOS如何决定java环境

《一篇文章彻底搞懂macOS如何决定java环境》MacOS作为一个功能强大的操作系统,为开发者提供了丰富的开发工具和框架,下面:本文主要介绍macOS如何决定java环境的相关资料,文中通过代码... 目录方法一:使用 which命令方法二:使用 Java_home工具(Apple 官方推荐)那问题来了,

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

Nginx分布式部署流程分析

《Nginx分布式部署流程分析》文章介绍Nginx在分布式部署中的反向代理和负载均衡作用,用于分发请求、减轻服务器压力及解决session共享问题,涵盖配置方法、策略及Java项目应用,并提及分布式事... 目录分布式部署NginxJava中的代理代理分为正向代理和反向代理正向代理反向代理Nginx应用场景

vite搭建vue3项目的搭建步骤

《vite搭建vue3项目的搭建步骤》本文主要介绍了vite搭建vue3项目的搭建步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1.确保Nodejs环境2.使用vite-cli工具3.进入项目安装依赖1.确保Nodejs环境