概述
springboot整合activiti使用modeler进行流程创建,编辑、部署以及删除实例(可运行)
详细
1、现在来说一下流程,先建立spring boot项目,导入对应的jar包。
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.activiti</groupId><artifactId>activiti-spring-boot-starter-basic</artifactId><version>5.22.0</version></dependency><dependency><groupId>org.activiti</groupId><artifactId>activiti-diagram-rest</artifactId><!-- exclusions:activiti-diagram-rest中含有security的jar包,导致访问springbott需要登录,此处剔除登录--><exclusions><exclusion><artifactId>spring-security-config</artifactId><groupId>org.springframework.security</groupId></exclusion><exclusion><artifactId>spring-security-core</artifactId><groupId>org.springframework.security</groupId></exclusion><exclusion><artifactId>spring-security-crypto</artifactId><groupId>org.springframework.security</groupId></exclusion><exclusion><artifactId>spring-security-web</artifactId><groupId>org.springframework.security</groupId></exclusion></exclusions><version>5.22.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.activiti</groupId><artifactId>activiti-common-rest</artifactId><version>5.19.0</version></dependency><dependency><groupId>org.activiti</groupId><artifactId>activiti-json-converter</artifactId><version>5.22.0</version></dependency><dependency><groupId>org.activiti</groupId><artifactId>activiti-modeler</artifactId><exclusions><exclusion><artifactId>spring-security-config</artifactId><groupId>org.springframework.security</groupId></exclusion><exclusion><artifactId>spring-security-core</artifactId><groupId>org.springframework.security</groupId></exclusion><exclusion><artifactId>spring-security-crypto</artifactId><groupId>org.springframework.security</groupId></exclusion><exclusion><artifactId>spring-security-web</artifactId><groupId>org.springframework.security</groupId></exclusion></exclusions><version>5.19.0</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.58</version></dependency><!-- https://mvnrepository.com/artifact/batik/batik-script --></dependencies>
2、从官方发布的activiti-explorer.war包中获取这几个静态资源文件,并复制到对应的目录,如下:
再复制几个封装好的后台逻辑(保存流程模板等操作);
2、复制好后,在三个后台文件中分别加入@RequestMapping("/service")路径限制,以免与jar的controller路径冲突:
修改resources\static\editor-app\app-cfg.js文件,指定为刚刚我们复制的三个文件的访问路径
其中,stencilset.json文件为我门编辑器的界面控件配置,这个可以自己修改设定。可以增加减少自己实际业务中需要的控件及属性.
3.此时设置我们的数据库链接配置:
server.port=8888
spring.datasource.url=jdbc:mysql://localhost:3306/activitiDemo?characterEncoding=UTF-8&nullCatalogMeansCurrent=true&serverTimezone=GMT&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##每次应用启动不检查Activiti数据表是否存在及版本号是否匹配 第一次设置为true 会自动创建表结构,之后即可设置为false 提高运行速度
spring.activiti.database-schema-update=true
4、创建maincontroller,开启我们的访问界面入口:
@Autowiredprivate RepositoryService repositoryService;@Autowiredprivate ObjectMapper objectMapper;@Autowiredprivate FormService formService;@Autowiredprivate RuntimeService runtimeService;@Autowiredprivate TaskService taskService;@Autowiredprivate HistoryService historyService;@Autowiredprivate ProcessEngineConfiguration processEngineConfiguration;/*** 创建模型* @param request* @param response* @return*/@RequestMapping("/createModel")public String createModel(HttpServletRequest request, HttpServletResponse response) {String name = "请假流程";String description = "这是一个请假流程";String id = null;try {Model model = repositoryService.newModel();String key = name;//版本号String revision = "1";ObjectNode modelNode = objectMapper.createObjectNode();modelNode.put(ModelDataJsonConstants.MODEL_NAME, name);modelNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);modelNode.put(ModelDataJsonConstants.MODEL_REVISION, revision);model.setName(name);model.setKey(key);//模型分类 结合自己的业务逻辑//model.setCategory(category);model.setMetaInfo(modelNode.toString());repositoryService.saveModel(model);id = model.getId();//完善ModelEditorSourceObjectNode 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);repositoryService.addModelEditorSource(id, editorNode.toString().getBytes("utf-8"));String aa = request.getContextPath();response.sendRedirect(request.getContextPath() + "/modeler.html?modelId=" + id);} catch (Exception e) {e.printStackTrace();}return "index";}
最后启动项目:访问http://localhost:8888/createModel,即可进入到文章开头的编辑器主页
5、创建之后肯定要保存,这就用到了框架自带的保存ModelSaveRestResource类,这里需要注意的是,在实际保存的时候,很可能会报错500,或者403之类的,说明是参数不匹配,这就是我自己踩的一个坑,需要自己修改,把参数展开来入下,如本身没问题,可忽略这一步:
public void saveModel(@PathVariable String modelId, String name, String description, String json_xml,String svg_xml) { }
6.基本的流程能跑起来之后,上面所说的已经包含了运用modeler创建与保存实例,接下来,还有部署,删除,开启,流程跟踪等步骤,因网上很多都是单元测试实例,对初学者很不友好,所以自己整合了一些简单的界面来演示这些流程,因代码太杂,就直接说明使用流程,并粘贴代码:
具体得自己操作看看,代码会粘贴出来,可自己访问操作http://localhost:8888/index。activiti的数据库,如没有表,会自动创建表,表的说明可自行百度,这部分文章倒是挺多的。