Springboot整合Flowable6.x导出bpmn20的步骤详解

2023-05-16 12:05:25 步骤 导出 详解

项目源码仓库

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

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

导入Maven依赖

        <dependency>
            <groupId>org.flowable</groupId>
            <artifactId>flowable-spring-boot-starter-basic</artifactId>
            <version>6.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.flowable</groupId>
            <artifactId>flowable-JSON-converter</artifactId>
            <version>6.4.1</version>
        </dependency>

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

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

@Service
public class MyModelServiceImpl implements MyModelService {
    @Autowired
    private RepositoryService repositoryService;

    
    @Override
    public ResultDTO genXml(String guid) throws Exception {
        
        Model modelData = repositoryService.getModel(guid);
        byte[] bytes = repositoryService.getModelEditorSource(modelData.getId());
        if (bytes == null) {
            return ResultDTO.failureCustom("模型数据为空,请先设计流程并成功保存,再进行发布。");
        }
        jsonnode modelNode = new ObjectMapper().readTree(bytes);
        BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);
        if (model.getProcesses().size() == 0) {
            return ResultDTO.failureCustom("数据模型不符要求,请至少设计一条主线流程。");
        }
        
        model.getMainProcess().setName(modelData.getName());
        
        if(StringUtils.isNotBlank(modelData.getCateGory())) {
            model.setTargetNamespace(modelData.getCategory());
        }
        byte[] bpmnBytes = new BpmnXMLConverter().convertToXML(model);
        String xml = new String(bpmnBytes, "UTF-8");
        return ResultDTO.success(xml);
    }
}

运行效果如下:

{% asset_img res1.gif 导出效果 %}

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

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

@RestController
@Slf4j
public class ProcessController {
    @Autowired
    private MyProcessService processService;
    
    
    @GetMapping(value = "/res/exp")
    @apiOperation("通过processDefinition.id和resType导出流程XML或图片资源")
    public void resourceRead(@RequestParam("id") String id,@RequestParam("resType") String resType, httpservletResponse response) throws Exception {
        
        InputStream resourceAsStream = processService.resourceRead(id,resType);
        byte[] b = new byte[1024];
        int len = -1;
        while ((len = resourceAsStream.read(b, 0, 1024)) != -1) {
            response.getOutputStream().write(b, 0, len);
        }
    }
}
@Service
public class MyProcessServiceImpl implements MyProcessService {
    @Autowired
    private RepositoryService repositoryService;
    
    @Override
    public InputStream resourceRead(String id, String resType) throws Exception {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(id).singleResult();
        String resourceName = "";
        if (resType.equals("image/png")) {
            resourceName = processDefinition.getDiagramResourceName();
        } else if (resType.equals("text/xml")) {
            resourceName = processDefinition.getResourceName();
        }
        InputStream resourceAsStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), resourceName);
        return resourceAsStream;
    }
}

运行效果如下:

项目源码仓库

到此这篇关于SpringBoot整合Flowable6.x导出bpmn20的文章就介绍到这了,更多相关Springboot整合Flowable内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关文章