经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring Boot » 查看文章
Springboot整合Flowable6.x导出bpmn20
来源:cnblogs  作者:soft1314  时间:2023/4/19 9:02:28  对本文有异议

项目源码仓库

BPMN2.0(Business Process Model and Notation)是一套业务流程模型与符号建模标准,以XML为载体,以符号可视化业务,支持精准的执行语义来描述元素的操作。
Flowable诞生于Activiti,是一个使用Java编写的轻量级业务流程引擎。Flowable流程引擎可用于部署BPMN 2.0流程定义,可以十分灵活地加入你的应用/服务/构架。

本文给出两种从flowable导出流程定义bpmn20.xml的方式。

导入Maven依赖

  1. <dependency>
  2. <groupId>org.flowable</groupId>
  3. <artifactId>flowable-spring-boot-starter-basic</artifactId>
  4. <version>6.4.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.flowable</groupId>
  8. <artifactId>flowable-json-converter</artifactId>
  9. <version>6.4.1</version>
  10. </dependency>

从流程模型导出流程定义bpmn20.xml

通过流程编辑器制作的流程模型(如下图所示), 可以通过模型ID(Model.id),调用flowable 的 RepositoryService 来生成bpmn20.xml。

  1. @Service
  2. public class MyModelServiceImpl implements MyModelService {
  3. @Autowired
  4. private RepositoryService repositoryService;
  5. /**
  6. * 通过模型ID,生成模型BPMN20.xml
  7. * @param guid 模型id,即model.id
  8. * @return
  9. * @throws Exception
  10. */
  11. @Override
  12. public ResultDTO genXml(String guid) throws Exception {
  13. /**通过ID获取模型 **/
  14. Model modelData = repositoryService.getModel(guid);
  15. byte[] bytes = repositoryService.getModelEditorSource(modelData.getId());
  16. if (bytes == null) {
  17. return ResultDTO.failureCustom("模型数据为空,请先设计流程并成功保存,再进行发布。");
  18. }
  19. JsonNode modelNode = new ObjectMapper().readTree(bytes);
  20. BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);
  21. if (model.getProcesses().size() == 0) {
  22. return ResultDTO.failureCustom("数据模型不符要求,请至少设计一条主线流程。");
  23. }
  24. /** 设置名称 **/
  25. model.getMainProcess().setName(modelData.getName());
  26. /** 设置 targetNamespace **/
  27. if(StringUtils.isNotBlank(modelData.getCategory())) {
  28. model.setTargetNamespace(modelData.getCategory());
  29. }
  30. byte[] bpmnBytes = new BpmnXMLConverter().convertToXML(model);
  31. String xml = new String(bpmnBytes, "UTF-8");
  32. return ResultDTO.success(xml);
  33. }
  34. }

运行效果如下:
{% asset_img res1.gif 导出效果 %}

从流程定义导出流程定义bpmn20.xml

对于flowable已经部署的流程,可根据流程定义(ProcessDefinition.id),调用flowable 的RepositoryService来导出其bpmn20.xml。

  1. @RestController
  2. @Slf4j
  3. public class ProcessController {
  4. @Autowired
  5. private MyProcessService processService;
  6. /**
  7. * 通过processDefinition.id和resType导出流程XML或图片资源
  8. * @param id processDefinition.id
  9. * @param resType 取值 “image/png”或“text/xml”
  10. * @param response
  11. * @throws Exception
  12. */
  13. @GetMapping(value = "/res/exp")
  14. @ApiOperation("通过processDefinition.id和resType导出流程XML或图片资源")
  15. public void resourceRead(@RequestParam("id") String id,@RequestParam("resType") String resType, HttpServletResponse response) throws Exception {
  16. /** resType取值 “image/png”或“text/xml” **/
  17. InputStream resourceAsStream = processService.resourceRead(id,resType);
  18. byte[] b = new byte[1024];
  19. int len = -1;
  20. while ((len = resourceAsStream.read(b, 0, 1024)) != -1) {
  21. response.getOutputStream().write(b, 0, len);
  22. }
  23. }
  24. }
  1. @Service
  2. public class MyProcessServiceImpl implements MyProcessService {
  3. @Autowired
  4. private RepositoryService repositoryService;
  5. @Override
  6. public InputStream resourceRead(String id, String resType) throws Exception {
  7. ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(id).singleResult();
  8. String resourceName = "";
  9. if (resType.equals("image/png")) {
  10. resourceName = processDefinition.getDiagramResourceName();
  11. } else if (resType.equals("text/xml")) {
  12. resourceName = processDefinition.getResourceName();
  13. }
  14. InputStream resourceAsStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), resourceName);
  15. return resourceAsStream;
  16. }
  17. }

运行效果如下:

项目源码仓库

原文链接:https://www.cnblogs.com/soft1314/p/17331429.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号