一、集成准备

下载activiti源码及activiti-5.22.zip

  1. 源码下载
  1. Activiti-5.22.0.zip下载

二、复制项目文件到自己的项目中

  1. 进入源码文件中的modules\activiti-webapp-explorer2\src\main\webapp目录,复制diagram-viewereditor-appmodeler.html三个文件到springboot项目中的resources目录下。(主要是前端页面)
  2. 解压activiti-5.22.0.zip,在Activiti-5.22.0的libs中找到activiti-modeler-5.22.0-sources.jar,将其解压,将会找到以下三个类:(三个controller)
  3. 将以下三个类复制到自己的springboot项目的包中,只要包是位于启动类的下层包即可,这样springboot就会自动扫描到这三个类。
  4. 将源码路径modules\activiti-webapp-explorer2\src\main\resources\stencilset.json复制到springboot项目中的resources目录下。(这个文件是控制界面的语言的,附一个中文的文件下载地址stencilest.json-中文
  5. 至此,所有需要的文件都复制完成,接下来,我们开始配置修改。项目结构如下:

三、修改配置

1.添加依赖,并刷新项目(这是Gradle的,Maven自己转换一下)

dependencies {
        compile('org.activiti:activiti-spring-boot-starter-basic:5.22.0')
        compile("org.springframework.boot:spring-boot-starter-web")
        testCompile('org.springframework.boot:spring-boot-starter-test')
        compile('org.activiti:activiti-modeler:5.22.0')
        compile('commons-io:commons-io:2.5')
    }

2.修改springboot的启动类, 在类上加如下注解,关闭security功能。主要是因为activiti的依赖中含有spring security的jar包,所以springboot会自动配置安全功能,访问时就需要输入密码。

@EnableAutoConfiguration(exclude = {
        org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class,
        org.activiti.spring.boot.SecurityAutoConfiguration.class,
})

3.修改 resources目录下的app-cfg.js, 如下更改(这个是项目的路径,springboot是不需要项目路径的,所以把项目路径删除掉)

ACTIVITI.CONFIG = {
    'contextRoot' : '/service',
};

4.还记得我们前面复制进来的三个类吗?

在每个类的类名上加上RequestMapping(value = “/service”)注解。(页面会访问这个路径)

5.修改ModelSaveRestResource
(不修改这个的话,在页面保存流程时会报错)

@RestController
@RequestMapping(value = "/service")
public class ModelSaveRestResource implements ModelDataJsonConstants {

  protected static final Logger LOGGER = LoggerFactory.getLogger(ModelSaveRestResource.class);

  @Autowired
  private RepositoryService repositoryService;

  @Autowired
  private ObjectMapper objectMapper;

  @RequestMapping(value="/model/{modelId}/save", method = RequestMethod.PUT)
  @ResponseStatus(value = HttpStatus.OK)
  public void saveModel(@PathVariable String modelId, @RequestParam("name") String name,
                        @RequestParam("json_xml") String json_xml, 
                        @RequestParam("svg_xml") String svg_xml,
                        @RequestParam("description") String description) {//对接收参数进行了修改
    try {

      Model model = repositoryService.getModel(modelId);

      ObjectNode modelJson = (ObjectNode) objectMapper.readTree(model.getMetaInfo());

      modelJson.put(MODEL_NAME, name);
      modelJson.put(MODEL_DESCRIPTION, description);
      model.setMetaInfo(modelJson.toString());
      model.setName(name);

      repositoryService.saveModel(model);

      repositoryService.addModelEditorSource(model.getId(), json_xml.getBytes("utf-8"));

      InputStream svgStream = new ByteArrayInputStream(svg_xml.getBytes("utf-8"));
      TranscoderInput input = new TranscoderInput(svgStream);

      PNGTranscoder transcoder = new PNGTranscoder();
      // Setup output
      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
      TranscoderOutput output = new TranscoderOutput(outStream);

      // Do the transformation
      transcoder.transcode(input, output);
      final byte[] result = outStream.toByteArray();
      repositoryService.addModelEditorSourceExtra(model.getId(), result);
      outStream.close();

    } catch (Exception e) {
      LOGGER.error("Error saving model", e);
      throw new ActivitiException("Error saving model", e);
    }
  }
}

四、编写测试

新建一个controller如下

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author kingboy--KingBoyWorld@163.com
 * @date 2017/8/22 下午6:00
 * @desc .
 */
@RestController
@RequestMapping("/activiti")
public class ActivitiController {
    /**
     * 创建模型
     */
    @RequestMapping("/create")
    public void create(HttpServletRequest request, HttpServletResponse response) {
        try {
            ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

            RepositoryService repositoryService = processEngine.getRepositoryService();

            ObjectMapper objectMapper = new ObjectMapper();
            ObjectNode 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);
            Model modelData = repositoryService.newModel();

            ObjectNode modelObjectNode = objectMapper.createObjectNode();
            modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, "hello1111");
            modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
            String description = "hello1111";
            modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
            modelData.setMetaInfo(modelObjectNode.toString());
            modelData.setName("hello1111");
            modelData.setKey("12313123");

            //保存模型
            repositoryService.saveModel(modelData);
            repositoryService.addModelEditorSource(modelData.getId(), editorNode.toString().getBytes("utf-8"));
            response.sendRedirect(request.getContextPath() + "/modeler.html?modelId=" + modelData.getId());
        } catch (Exception e) {
            System.out.println("创建模型失败:");
        }
    }
}

启动项目访问localhost:8080/activiti/create,看看是不是可以看到编辑页面了呢?
配置过程一定要细心,稍微不小心就会失败,其中有很多坑,慢慢解决就好