Integrating Spring Boot with Activiti: Common Operations and REST APIs
This tutorial demonstrates how to integrate Spring Boot with the Activiti workflow engine, covering essential operations such as querying tasks, suspending, activating, and deleting process instances, and provides complete Java code snippets and controller endpoints for each function.
This article builds on the previous three parts about Spring Boot integration with the Activiti workflow engine and presents a collection of frequently used operations.
Query tasks owned by a given user
/**
* <p>Query tasks owned by a given user</p>
* <p>Time: 2021-01-31 11:17:49</p>
* @author xg
* @param userId User ID (task initiator)
* @return List<HistoricProcessInstance>
*/
public List<HistoricProcessInstance> ownerTasks(String userId) {
HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery();
List<HistoricProcessInstance> list = query.involvedUser(userId).list();
return list;
}Controller endpoint:
/**
* <p>Query tasks owned by a given user</p>
* <p>Time: 2021-01-31 11:19:07</p>
* @author xg
* @param userId
* @return R
*/
@GetMapping("/{userId}")
public R myMission(@PathVariable("userId") String userId) {
return R.success(holidayService.ownerTasks(userId));
}Access the API via /holidays/10000. If the returned endTime is null, the task has no result.
Query tasks assigned to a user
/**
* <p>Query tasks assigned to a user</p>
* <p>Time: 2021-01-23 11:39:56</p>
* @author xg
* @param assignee Assigned user
* @return List<Task>
*/
public List<Task> queryTasks(String assignee) {
TaskQuery query = taskService.createTaskQuery();
return query.taskAssignee(assignee).orderByTaskCreateTime().asc().list();
}Controller endpoint:
@GetMapping("/tasks")
public R myTasks(String userId) {
List<Task> list = holidayService.queryTasks(userId);
List<Map<String, Object>> result = list.stream().map(task -> {
Map<String, Object> res = new HashMap<>();
res.put("id", task.getId());
res.put("assignee", task.getAssignee());
res.put("createTime", task.getCreateTime());
res.put("bussinessKey", task.getBusinessKey());
res.put("category", task.getCategory());
res.put("dueDate", task.getDueDate()); // due date
res.put("desc", task.getDescription());
res.put("name", task.getName());
res.put("owner", task.getOwner());
res.put("instanceId", task.getProcessInstanceId());
res.put("variables", task.getProcessVariables());
res.put("state", task.isSuspended());
return res;
}).collect(Collectors.toList());
return R.success(result);
}Image illustrating the task list response:
Suspend a process instance
@GetMapping("/suspend")
public R suspendProcessInstance(String processInstanceId) {
holidayService.suspendProcess(processInstanceId);
return R.success();
}Service method:
/**
* <p>Suspend a process</p>
* <p>Time: 2021-01-29 16:10:44</p>
* @author xg
* @param processInstanceId
*/
public void suspendProcess(String processInstanceId) {
runtimeService.suspendProcessInstanceById(processInstanceId);
}Image showing suspension result:
Activate a suspended process
/**
* <p>Activate a process</p>
* <p>Time: 2021-01-29 16:20:54</p>
* @author xg
* @param processInstanceId
*/
public void activateProcess(String processInstanceId) {
runtimeService.activateProcessInstanceById(processInstanceId);
}Controller endpoint:
@GetMapping("/activate")
public R activateProcessInstance(String processInstanceId) {
holidayService.activateProcess(processInstanceId);
return R.success();
}Image showing activation result:
Delete a process instance
/**
* <p>Delete a process</p>
* <p>Time: 2021-01-29 15:50:08</p>
* @author xg
* @param processInstanceId
*/
public void stopProcess(String processInstanceId) {
runtimeService.deleteProcessInstance(processInstanceId, "Approval error");
}Controller endpoint:
@GetMapping("/remove")
public R removeProcessInstance(String processInstanceId) {
holidayService.stopProcess(processInstanceId);
return R.success();
}Images showing the deletion workflow and final screenshots:
The tutorial concludes with all common Activiti operations demonstrated and ready for further extensions such as termination and rejection handling.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
