使用Spring Boot示例实现Activiti REST API
I. Activiti REST API概述
Activiti Engine包含一个REST API,可以通过以下方式使用:
- 将activiti-rest.war文件部署到servlet容器,
- 或者包括servlet及其在应用程序中的映射,并将所有的activiti-rest依赖项添加到类路径中。
默认情况下,Activiti Engine将连接到内存中的H2数据库,因此使用Spring Boot,我们只需要添加依赖项,如下所示:
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </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>spring-boot-starter-rest-api</artifactId> <version>5.17.0</version> </dependency>
Activiti REST API支持:
- 部署:获取/创建/删除部署,获取部署中的资源。
流程定义:获取流程定义的一个或列表,获取流程定义的资源内容或BPMN模型,激活/暂停,获取/添加/删除候选启动器。
模型:获取/更新/创建/删除模型,获取/设置模型的编辑器源。
流程实例:获取/删除/激活/暂停流程实例,添加/删除用户,获取/创建/更新变量。
执行:获取执行,执行操作或获取活动活动,查询,获取/创建/更新变量。
任务:获取/更新/删除任务,查询任务,获取/创建/更新变量,获取/创建/删除身份链接,获取事件,获取/创建/删除附件。
历史记录:get / delete / query for Historic Process Instances,Task Instances,Activities Instances,Variables Intances。
用户和组:获取/创建/更新/删除信息。
数据库表,引擎,运行时,作业 ......
有关更多详细信息,请访问:Activiti用户指南 - REST API
在示例中,我们将测试其中一些以查看其工作原理。
II。实践
1.技术
- Java 1.8
- Maven 3.3.9
- Spring Tool Suite - 版本3.8.4.RELEASE
- Spring Boot:1.5.3.RELEASE
2.一步一步
2.1创建Spring Boot项目
使用Spring Tool Suite / Eclipse创建Project并将依赖项添加到pom.xml文件:
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </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>spring-boot-starter-rest-api</artifactId> <version>5.17.0</version> </dependency>
2.2定义过程
在src / main / resources下,创建processes文件夹并添加simple-process.bpmn20.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" targetNamespace="Examples"> <process id="simpleProcess" name="Simple Process"> <startEvent id="theStart" /> <sequenceFlow sourceRef="theStart" targetRef="theTask" /> <userTask id="theTask" name="Task" activiti:assignee="${person}"> <documentation> Do the task. </documentation> </userTask> <sequenceFlow sourceRef="theTask" targetRef="theEnd" /> <endEvent id="theEnd" /> </process> </definitions>
我们使用${person}expression和activiti:assignee属性来分配任务。
2.3初始化用户
默认情况下,所有Activiti REST资源都需要对有效用户进行身份验证。所以我们应该创建一个管理员用户。打开Application类,添加Bean以初始化User:
package com.javasampleapproach.restactiviti; import org.activiti.engine.IdentityService; import org.activiti.engine.identity.User; import org.springframework.beans.factory.InitializingBean; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class SpringRestActivitiApplication { public static void main(String[] args) { SpringApplication.run(SpringRestActivitiApplication.class, args); } @Bean InitializingBean usersAndGroupsInitializer(final IdentityService identityService) { return new InitializingBean() { public void afterPropertiesSet() throws Exception { User admin = identityService.newUser("admin"); admin.setPassword("admin"); identityService.saveUser(admin); } }; } }
使用基本HTTP访问身份验证,因此我们始终在请求URL中包含授权用户名和密码。
有关Activiti服务的更多信息,请参见:Activiti简介 - 一个Java BPM引擎
2.4运行并检查结果
- Config maven build:
clean install
- 使用Spring Boot App模式运行项目
- 检查结果:
部署清单:
curl -u admin:admin http://localhost:8080/repository/deployments { "data": [ { "id": "1", "name": "SpringAutoDeployment", "deploymentTime": "2017-05-15T20:08:26.859+07:00", "category": null, "url": "http://localhost:8080/repository/deployments/1", "tenantId": "" } ], "total": 1, "start": 0, "sort": "id", "order": "asc", "size": 1 }
列出部署中的资源:
curl -u admin:admin http://localhost:8080/repository/deployments/1/resources [ { "id": "E:\STS\WorkPlace\SpringRestActiviti\target\classes\processes\simple-process.bpmn20.xml", "url": "http://localhost:8080/repository/deployments/1/resources/E:\STS\WorkPlace\SpringRestActiviti\target\classes\processes\simple-process.bpmn20.xml", "contentUrl": "http://localhost:8080/repository/deployments/1/resourcedata/E:\STS\WorkPlace\SpringRestActiviti\target\classes\processes\simple-process.bpmn20.xml", "mediaType": "text/xml", "type": "processDefinition" } ]
流程定义列表:
curl -u admin:admin http://localhost:8080/repository/process-definitions { "data": [ { "id": "simpleProcess:1:3", "url": "http://localhost:8080/repository/process-definitions/simpleProcess:1:3", "key": "simpleProcess", "version": 1, "name": "Simple Process", "description": null, "tenantId": "", "deploymentId": "1", "deploymentUrl": "http://localhost:8080/repository/deployments/1", "resource": "http://localhost:8080/repository/deployments/1/resources/E:\STS\WorkPlace\SpringRestActiviti\target\classes\processes\simple-process.bpmn20.xml", "diagramResource": null, "category": "Examples", "graphicalNotationDefined": false, "suspended": false, "startFormDefined": false } ], "total": 1, "start": 0, "sort": "name", "order": "asc", "size": 1 }
获取BPMN模型(通过processId):
curl -u admin:admin http://localhost:8080/repository/process-definitions/simpleProcess:1:3/model { "definitionsAttributes": {}, "processes": [ { "id": "simpleProcess", "xmlRowNumber": 5, "xmlColumnNumber": 52, "extensionElements": {}, "attributes": {}, "name": "Simple Process", "executable": true, "documentation": null, "ioSpecification": null, "executionListeners": [], "lanes": [], "dataObjects": [], "candidateStarterUsers": [], "candidateStarterGroups": [], "eventListeners": [], "flowElements": [ { "id": "theStart", "xmlRowNumber": 6, "xmlColumnNumber": 31, ... "outgoingFlows": [ { "id": null, "xmlRowNumber": 7, "xmlColumnNumber": 60, ... "sourceRef": "theStart", "targetRef": "theTask", } ], "eventDefinitions": [], "initiator": null, "formKey": null, "formProperties": [] }, { "id": null, "xmlRowNumber": 7, "xmlColumnNumber": 60, ... "sourceRef": "theStart", "targetRef": "theTask", }, { "id": "theTask", "xmlRowNumber": 9, "xmlColumnNumber": 68, ... "name": "Task", "documentation": "Do the task.", "incomingFlows": [ { "id": null, "xmlRowNumber": 7, "xmlColumnNumber": 60, ... "sourceRef": "theStart", "targetRef": "theTask", } ], "outgoingFlows": [ { "id": null, "xmlRowNumber": 15, "xmlColumnNumber": 58, ... "sourceRef": "theTask", "targetRef": "theEnd", } ], ... "assignee": "${person}", }, { "id": null, "xmlRowNumber": 15, "xmlColumnNumber": 58, ... "sourceRef": "theTask", "targetRef": "theEnd", }, { "id": "theEnd", "xmlRowNumber": 16, "xmlColumnNumber": 27, ... "incomingFlows": [ { "id": null, "xmlRowNumber": 15, "xmlColumnNumber": 58, ... "sourceRef": "theTask", "targetRef": "theEnd", } ], "outgoingFlows": [], "eventDefinitions": [] } ], "artifacts": [] } ], ... "resources": [], "targetNamespace": "Examples", "mainProcess": { ... ], "artifacts": [] }, "itemDefinitions": {}, "namespaces": { "activiti": "http://activiti.org/bpmn" } }
开始流程:
curl -u admin:admin -H "Content-Type: application/json" -d '{"processDefinitionKey":"simpleProcess", "variables": [ {"name":"person", "value":"John"}]}' http://localhost:8080/runtime/process-instances
我们还发送了变量信息{“person”:“John”}。此请求返回流程实例:
{ "id": "4", "url": "http://localhost:8080/runtime/process-instances/4", "businessKey": null, "suspended": false, "ended": false, "processDefinitionId": "simpleProcess:1:3", "processDefinitionUrl": "http://localhost:8080/repository/process-definitions/simpleProcess:1:3", "activityId": "theTask", "variables": [], "tenantId": "", "completed": false }
按流程实例ID列出的变量
curl -u admin:admin http://localhost:8080/runtime/process-instances/4/variables [ { "name": "person", "type": "string", "value": "John", "scope": "local" } ]
任务清单:
curl -u admin:admin http://localhost:8080/runtime/tasks { "data": [ { "id": "9", "url": "http://localhost:8080/runtime/tasks/9", "owner": null, "assignee": "John", "delegationState": null, "name": "Task", "description": "Do the task.", "createTime": "2017-05-15T20:28:22.667+07:00", "dueDate": null, "priority": 50, "suspended": false, "taskDefinitionKey": "theTask", "tenantId": "", "category": null, "formKey": null, "parentTaskId": null, "parentTaskUrl": null, "executionId": "4", "executionUrl": "http://localhost:8080/runtime/executions/4", "processInstanceId": "4", "processInstanceUrl": "http://localhost:8080/runtime/process-instances/4", "processDefinitionId": "simpleProcess:1:3", "processDefinitionUrl": "http://localhost:8080/repository/process-definitions/simpleProcess:1:3", "variables": [] } ], "total": 1, "start": 0, "sort": "id", "order": "asc", "size": 1 }
将变量添加到任务(通过Id):
curl -u admin:admin -H "Content-Type: application/json" -d '[{"name" : "newTaskVariable", "scope" : "local", "type" : "string", "value" : "This is variable Value"}]' http://localhost:8080/runtime/tasks/9/variables [ { "name": "newTaskVariable", "type": "string", "value": "This is variable Value", "scope": "local" } ]
身份链接(由Id):
curl -u admin:admin http://localhost:8080/runtime/tasks/9/identitylinks [ { "url": "http://localhost:8080/runtime/tasks/9/identitylinks/users/John/assignee", "user": "John", "group": null, "type": "assignee" } ]
完成任务(由Id):
curl -u admin:admin -H "Content-Type: application/json" -d '{"action" : "complete"}' http://localhost:8080/runtime/tasks/9
历史流程实例:
curl -u admin:admin http://localhost:8080/history/historic-process-instances/4 { "id": "4", "url": "http://localhost:8080/history/historic-process-instances/4", "businessKey": null, "processDefinitionId": "simpleProcess:1:3", "processDefinitionUrl": "http://localhost:8080/repository/process-definitions/simpleProcess:1:3", "startTime": "2017-05-15T20:40:34.504+07:00", "endTime": "2017-05-15T20:44:51.036+07:00", "durationInMillis": 256532, "startUserId": "admin", "startActivityId": "theStart", "endActivityId": "theEnd", "deleteReason": null, "superProcessInstanceId": null, "variables": [], "tenantId": "" }