activiti流程设计器和rest api接口的搭建

备注:此文档是在2016年整理,没在网上发布,一直在是本地word中记录,现在迁移到线上。并且文章中是基于xml等文件配置的构建项目,所以感觉很繁琐。目前可以用springboot替代,并且activiti与springboot整合也是非常简单的。

1.maven项目的简单介绍

(1).首先在eclipse中创建一个maven项目,创建完maven项目之后,最重要的一条就是文件同步,文件同步需要在eclipse中添加一个文件同步插件叫filesync 。不要和build path中的source目录搞混。(想想这样应该可以达到热部署的效果)

2.开始activiti流程设计器和rest api接口的搭建

(1).首先引入需要的jar包,此项目用maven管理,所以编写pom.xml文件,pom.xml文件内容如下

"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  4.0.0


  com.tzxx

  activiti-web-modeler

  war

  0.0.1-SNAPSHOT


  activiti-web-modeler Maven Webapp

  http://maven.apache.org


  

  

4.1.9.RELEASE

1.0.18

5.21.0

1.7.7

2.2.3

5.1.30


1.8


  


  

 

org.springframework

spring-core

${spring.version}

commons-logging

commons-logging

org.springframework

spring-beans

${spring.version}

org.springframework

spring-context

${spring.version}

org.springframework

spring-context-support

${spring.version}


org.springframework

spring-tx

${spring.version}

     com.alibaba

     druid

     ${druid.version}

mysql

mysql-connector-java

${mysql.driver.version}

runtime


org.springframework

spring-web

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-oxm

${spring.version}

taglibs

standard

1.1.2

jar

javax.servlet

jstl

1.2

jar

javax.servlet

servlet-api

2.5

provided

javax.servlet.jsp

jsp-api

2.1

provided

org.activiti

activiti-engine

${activiti.version}

org.activiti

activiti-spring

${activiti.version}

       

        commons-dbcp

        commons-dbcp

       

       

org.activiti

activiti-explorer

${activiti.version}

vaadin

com.vaadin

dcharts-widget

org.vaadin.addons

activiti-simple-workflow

org.activiti

org.activiti

activiti-modeler

           ${activiti.version}

org.activiti

activiti-diagram-rest

${activiti.version}

    org.activiti

    activiti-rest

    ${activiti.version}

     org.activiti

     activiti-json-converter

     ${activiti.version}

    

    

     commons-collections

     commons-collections

    

    

    

org.slf4j

slf4j-api

${slf4j.version}

org.slf4j

slf4j-log4j12

${slf4j.version}


org.slf4j

jcl-over-slf4j

${slf4j.version}


org.slf4j

jul-to-slf4j

${slf4j.version}

com.fasterxml.jackson.core

jackson-core

${jackson.version}

com.fasterxml.jackson.core

jackson-databind

${jackson.version}

com.fasterxml.jackson.core

jackson-annotations

${jackson.version}

com.fasterxml.jackson.module

jackson-module-jaxb-annotations

${jackson.version}

    

      junit

      junit

      4.11

      test

    

  


  

    activiti-web-modeler

  


(2).pom文件编写好之后,开始搭建Spring环境和springmvc的环境,因为activiti的项目时基于这两框架开发的所以在此也用这两框架作为基础来管理bean。首先创建Spring的配置文件beans.xml,其中的内包括基础bean的配置和数据库连接池的配置等,我在整合的过程中将activiti的配置文件信息也直接写到了bean.xml文件中了,其中的信息包括初始化activiti数据库属性配置,事务的配置等等一系列。以下图片强调几个需要注意的问题,(在搭建过程遇到的问题)。



具体的bean.xml的内容如下(其中连接池用的是alibaba的)

"1.0" encoding="UTF-8"?>

"http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  

xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd

http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd

http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd

http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"

default-lazy-init="true">


Spring Configuration


"true" location="classpath:activiti.properties" />

"transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

"dataSource" ref="dataSource" />


"transactionManager" proxy-target-class="true"/>



"dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"


    "driverClassName" value="${jdbc.driver}" />



"url" value="${jdbc.url}" />

"username" value="${jdbc.username}" />

"password" value="${jdbc.password}" />


"initialSize" value="${jdbc.pool.init}" />

"minIdle" value="${jdbc.pool.minIdle}" /> 

"maxActive" value="${jdbc.pool.maxActive}" />


"maxWait" value="60000" />


"timeBetweenEvictionRunsMillis" value="60000" />


"minEvictableIdleTimeMillis" value="300000" />

"testWhileIdle" value="true" />

"testOnBorrow" value="false" />

"testOnReturn" value="false" />

"org.activiti.conf,org.activiti.rest.editor,org.activiti.rest.service">

        "annotation" expression="org.springframework.stereotype.Controller"/>

    


    "restResponseFactory" class="org.activiti.rest.service.api.RestResponseFactory">


 

"objectMapper" class="com.fasterxml.jackson.databind.ObjectMapper" />


    "uuidGenerator" class="org.activiti.engine.impl.persistence.StrongUuidGenerator" />

"processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">

"dataSource" ref="dataSource" />

"transactionManager" ref="transactionManager" />

"databaseSchemaUpdate" value="true" />

"jobExecutorActivate" value="true" />

"history" value="full" />


"activityFontName" value="${activiti.diagram.activityFontName}"/>

"labelFontName" value="${activiti.diagram.labelFontName}"/>

"annotationFontName" value="${activiti.diagram.annotationFontName}"/>

"processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">

"processEngineConfiguration" ref="processEngineConfiguration" />

"repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />

"runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />

"formService" factory-bean="processEngine" factory-method="getFormService" />

"identityService" factory-bean="processEngine" factory-method="getIdentityService" />

"taskService" factory-bean="processEngine" factory-method="getTaskService" />

"historyService" factory-bean="processEngine" factory-method="getHistoryService" />

"managementService" factory-bean="processEngine" factory-method="getManagementService" />


"contentTypeResolver" class="org.activiti.rest.common.application.DefaultContentTypeResolver" />

(3).spring mvc.xml的配置文件内容(只是简单的内容,其他的属性配置根据需要配)

"1.0" encoding="UTF-8"?>

"http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">


    Spring MVC Configuration



    "org.activiti.rest.editor,org.activiti.rest.diagram">

        "annotation" expression="org.springframework.stereotype.Controller" />

    


    "org.tzxx.controller">

    


    "objectMapper" class="com.fasterxml.jackson.databind.ObjectMapper"/>

    


    "org.springframework.web.servlet.view.InternalResourceViewResolver">

"prefix" value="/WEB-INF/jsp/" />

"suffix" value=".jsp" />

(4).spring-rest.xml文件的配置内容

"1.0" encoding="UTF-8"?>

"http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">


    Spring MVC Configuration



    "org.activiti.rest">

        "annotation" expression="org.springframework.stereotype.Controller" />

    



    "objectMapper" class="com.fasterxml.jackson.databind.ObjectMapper"/>


    


注意:以上配置文件的内容主要扫描包的配置

(5).properties文件的配置信息

#oracle database settings

#jdbc.type=oracle

#jdbc.driver=oracle.jdbc.driver.OracleDriver

#jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl

#jdbc.username=jeesite

#jdbc.password=123456


#mysql database setting

jdbc.type=mysql

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/activiti_modeler?useUnicode=true&characterEncoding=utf-8

jdbc.username=root

jdbc.password=123



#pool settings

jdbc.pool.init=1

jdbc.pool.minIdle=3

jdbc.pool.maxActive=20


#============================#

#===== System settings ======#

#============================#



#\u5de5\u4f5c\u6d41\u8bbe\u7f6e

activiti.isSynActivitiIndetity=false

activiti.export.diagram.path=c:/activiti_diagram

#activiti font (windows font: \u5b8b\u4f53  linux font: simsun)

activiti.diagram.activityFontName=\u5b8b\u4f53

activiti.diagram.labelFontName=\u5b8b\u4f53

#5.21.0 \u65b0\u589e\u53c2\u6570 ,2016.06.23 \u8f66\u6811\u708e add

activiti.diagram.annotationFontName=\u5b8b\u4f53

#activiti\u5916\u90e8\u8868\u5355\u6839\u5730\u5740\u914d\u7f6e

activiti.form.server.url=

(6).上面的配置文件写好之后,配置web.xml文件来实例化上下文信息

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

 "http://java.sun.com/dtd/web-app_2_3.dtd" >


  Archetype Created Web Application



  

    contextConfigLocation

    classpath:beans.xml

  



    

       

      org.springframework.web.context.ContextLoaderListener

   

   


   

       org.springframework.web.util.IntrospectorCleanupListener

   



    

        encodingFilter

        org.springframework.web.filter.CharacterEncodingFilter

        

            encoding

            UTF-8

        

        

            forceEncoding

            true

        

    


    

        encodingFilter

        /*

    


  

  

    ModelRestServlet

    org.springframework.web.servlet.DispatcherServlet

    

        contextConfigLocation

        classpath:spring-mvc.xml

    

    1

  

  

    ModelRestServlet

    /service/*

  


  

    RestServlet

    org.springframework.web.servlet.DispatcherServlet

    

        contextConfigLocation

        classpath:spring-rest.xml

    

    1

  

  

    RestServlet

    /rest/*

  


(7).下载activiti5.21版本的源码包(5.17之前的版本源码中的目录结构和之后有点不一样),解压之后,找到modules目录,进去之后找activiti-webapp-explorer2在这个文件夹中找如下文件

将上面红框中的文件拷贝到自己项目的webapp目录下。

再继续找 src/main/resources/stencilset.json将这个json文件拷贝到resources文件夹下,其定义了bpmn的相关构成组件的信息。

(8).准备基础服务类

复制文件(https://github.com/henryyan/kft-activiti-demo/tree/master/src/main/java/org/activiti/explorer)里面的java文件到自己项目中。

(9).添加Rest安全认证组件SecurityConfiguration类

package org.tzxx.conf;


import org.activiti.rest.security.BasicAuthenticationProvider;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.security.authentication.AuthenticationProvider;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;

import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;

import org.springframework.security.config.http.SessionCreationPolicy;


@Configuration

@EnableWebSecurity

@EnableWebMvcSecurity

public class SecurityConfiguration extends WebSecurityConfigurerAdapter {


    @Bean

    public AuthenticationProvider authenticationProvider() {

        return new BasicAuthenticationProvider();

    }


    @Override

    protected void configure(HttpSecurity http) throws Exception {

        http.authenticationProvider(authenticationProvider())

            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()

            .csrf().disable()

            .authorizeRequests()

            .anyRequest().authenticated()

            .and()

            .httpBasic();

    }

}

(10).写一个简单的ModelController来实现页面之间的跳转

import java.io.ByteArrayInputStream;

import java.util.List;


import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import org.activiti.bpmn.converter.BpmnXMLConverter;

import org.activiti.bpmn.model.BpmnModel;

import org.activiti.editor.constants.ModelDataJsonConstants;

import org.activiti.editor.language.json.converter.BpmnJsonConverter;

import org.activiti.engine.RepositoryService;

import org.activiti.engine.repository.Deployment;

import org.activiti.engine.repository.Model;

import org.apache.commons.io.IOUtils;

import org.apache.commons.lang3.StringUtils;


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.support.RedirectAttributes;


import com.fasterxml.jackson.databind.JsonNode;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.node.ObjectNode;


/**

*流程模型控制器

 *

 */

@Controller

@RequestMapping(value = "/workflow/model")

public class ModelController {


  protected Logger logger = LoggerFactory.getLogger(getClass());


  @Autowired

  RepositoryService repositoryService;


  /**

*模型列表

   */

  @RequestMapping(value = "list")

  public ModelAndView modelList() {

    ModelAndView mav = new ModelAndView("workflow/model-list");

    List list = repositoryService.createModelQuery().list();

    mav.addObject("list", list);

    return mav;

  }


  /**

*模型列表

   */

  @RequestMapping(value = "index")

  public String index() {


    return "createModel";

  }


  /**

*创建模型

   */

  @RequestMapping(value = "create")

  public void create(@RequestParam("name") String name, @RequestParam("key") String key, @RequestParam("description") String description,

          HttpServletRequest request, HttpServletResponse response) {

    try {

      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, name);

      modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);

      description = StringUtils.defaultString(description);

      modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);

      modelData.setMetaInfo(modelObjectNode.toString());

      modelData.setName(name);

      modelData.setKey(StringUtils.defaultString(key));


      repositoryService.saveModel(modelData);

      repositoryService.addModelEditorSource(modelData.getId(), editorNode.toString().getBytes("utf-8"));


      response.sendRedirect(request.getContextPath() + "/modeler.html?modelId=" + modelData.getId());

    } catch (Exception e) {

logger.error("创建模型失败:", e);

    }

  }


  /**

*根据Model部署流程

   */

  @RequestMapping(value = "deploy/{modelId}")

  public String deploy(@PathVariable("modelId") String modelId, RedirectAttributes redirectAttributes) {

    try {

      Model modelData = repositoryService.getModel(modelId);

      ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(repositoryService.getModelEditorSource(modelData.getId()));

      byte[] bpmnBytes = null;


      BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);

      bpmnBytes = new BpmnXMLConverter().convertToXML(model);


      String processName = modelData.getName() + ".bpmn20.xml";

      Deployment deployment = repositoryService.createDeployment().name(modelData.getName()).addString(processName, new String(bpmnBytes)).deploy();

redirectAttributes.addFlashAttribute("message", "部署成功,部署ID=" + deployment.getId());

    } catch (Exception e) {

logger.error("根据模型部署流程失败:modelId={}", modelId, e);

    }

    return "redirect:/workflow/model/list";

  }


  /**

*导出model的xml文件

   */

  @RequestMapping(value = "export/{modelId}")

  public void export(@PathVariable("modelId") String modelId, HttpServletResponse response) {

    try {

      Model modelData = repositoryService.getModel(modelId);

      BpmnJsonConverter jsonConverter = new BpmnJsonConverter();

      JsonNode editorNode = new ObjectMapper().readTree(repositoryService.getModelEditorSource(modelData.getId()));

      BpmnModel bpmnModel = jsonConverter.convertToBpmnModel(editorNode);

      BpmnXMLConverter xmlConverter = new BpmnXMLConverter();

      byte[] bpmnBytes = xmlConverter.convertToXML(bpmnModel);


      ByteArrayInputStream in = new ByteArrayInputStream(bpmnBytes);

      IOUtils.copy(in, response.getOutputStream());

      String filename = bpmnModel.getMainProcess().getId() + ".bpmn20.xml";

      response.setHeader("Content-Disposition", "attachment; filename=" + filename);

      response.flushBuffer();

    } catch (Exception e) {

logger.error("导出model的xml文件失败:modelId={}", modelId, e);

    }

  }


}

以上步骤基本的activiti的web流程设计器和rest接口搭建基础工作就已经完毕。下面效果图:




3.流程设计器的汉化

1.先将zh-CN.json放入editor-app\i18n下


2.把editor-app\app.js中的’en’改成’zh-CN’

3.替换stencilset.json

4.如果你已访问过原先的英文项目,请清除缓存信息

参考学习博客地址:

1.https://yq.aliyun.com/articles/11821

2.http://www.kafeitu.me/activiti/2013/03/10/integrate-activiti-modeler.html?spm=5176.100239.blogcont11821.5.ry4pbv

3.https://yq.aliyun.com/articles/59998

4.http://www.cnblogs.com/lighten/p/5878169.html

你可能感兴趣的:(activiti流程设计器和rest api接口的搭建)