美文网首页
activiti流程设计器和rest api接口的搭建

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

作者: G__yuan | 来源:发表于2019-07-08 18:49 被阅读0次

    备注:此文档是在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文件内容如下

    <project xmlns="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">

      <modelVersion>4.0.0</modelVersion>

      <groupId>com.tzxx</groupId>

      <artifactId>activiti-web-modeler</artifactId>

      <packaging>war</packaging>

      <version>0.0.1-SNAPSHOT</version>

      <name>activiti-web-modeler Maven Webapp</name>

      <url>http://maven.apache.org</url>

      <properties>

       <!-- main version setting -->

    <spring.version>4.1.9.RELEASE</spring.version>

    <druid.version>1.0.18</druid.version>

    <activiti.version>5.21.0</activiti.version>

    <!-- tools version setting -->

    <slf4j.version>1.7.7</slf4j.version>

    <jackson.version>2.2.3</jackson.version>

    <!-- jdbc driver setting -->

    <mysql.driver.version>5.1.30</mysql.driver.version>

    <!-- environment setting -->

    <jdk.version>1.8</jdk.version>

      </properties>

      <dependencies>

      <!-- SPRING begin -->

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-core</artifactId>

    <version>${spring.version}</version>

    <exclusions>

    <exclusion>

    <groupId>commons-logging</groupId>

    <artifactId>commons-logging</artifactId>

    </exclusion>

    </exclusions>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-beans</artifactId>

    <version>${spring.version}</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    <version>${spring.version}</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context-support</artifactId>

    <version>${spring.version}</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-tx</artifactId>

    <version>${spring.version}</version>

    </dependency>

    <!-- PERSISTENCE begin -->

    <!-- connection pool -->

    <dependency>

         <groupId>com.alibaba</groupId>

         <artifactId>druid</artifactId>

         <version>${druid.version}</version>

    </dependency>

    <!-- jdbc driver -->

    <dependency>

    <groupId>mysql</groupId>

    <artifactId>mysql-connector-java</artifactId>

    <version>${mysql.driver.version}</version>

    <scope>runtime</scope>

    </dependency>

    <!-- PERSISTENCE end -->

    <!-- WEB begin -->

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-web</artifactId>

    <version>${spring.version}</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-webmvc</artifactId>

    <version>${spring.version}</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-oxm</artifactId>

    <version>${spring.version}</version>

    </dependency>

    <dependency>

    <groupId>taglibs</groupId>

    <artifactId>standard</artifactId>

    <version>1.1.2</version>

    <type>jar</type>

    </dependency>

    <dependency>

    <groupId>javax.servlet</groupId>

    <artifactId>jstl</artifactId>

    <version>1.2</version>

    <type>jar</type>

    </dependency>

    <dependency>

    <groupId>javax.servlet</groupId>

    <artifactId>servlet-api</artifactId>

    <version>2.5</version>

    <scope>provided</scope>

    </dependency>

    <dependency>

    <groupId>javax.servlet.jsp</groupId>

    <artifactId>jsp-api</artifactId>

    <version>2.1</version>

    <scope>provided</scope>

    </dependency>

    <!-- WEB end -->

    <!-- Activiti -->

    <dependency>

    <groupId>org.activiti</groupId>

    <artifactId>activiti-engine</artifactId>

    <version>${activiti.version}</version>

    </dependency>

    <dependency>

    <groupId>org.activiti</groupId>

    <artifactId>activiti-spring</artifactId>

    <version>${activiti.version}</version>

    <exclusions>

            <exclusion>

            <artifactId>commons-dbcp</artifactId>

            <groupId>commons-dbcp</groupId>

            </exclusion>

            </exclusions>

    </dependency>

    <dependency>

    <groupId>org.activiti</groupId>

    <artifactId>activiti-explorer</artifactId>

    <version>${activiti.version}</version>

    <exclusions>

    <exclusion>

    <artifactId>vaadin</artifactId>

    <groupId>com.vaadin</groupId>

    </exclusion>

    <exclusion>

    <artifactId>dcharts-widget</artifactId>

    <groupId>org.vaadin.addons</groupId>

    </exclusion>

    <exclusion>

    <artifactId>activiti-simple-workflow</artifactId>

    <groupId>org.activiti</groupId>

    </exclusion>

    </exclusions>

    </dependency>

    <dependency>

    <groupId>org.activiti</groupId>

    <artifactId>activiti-modeler</artifactId>

               <version>${activiti.version}</version>

    </dependency>

    <dependency>

    <groupId>org.activiti</groupId>

    <artifactId>activiti-diagram-rest</artifactId>

    <version>${activiti.version}</version>

    </dependency>

    <dependency>

        <groupId>org.activiti</groupId>

        <artifactId>activiti-rest</artifactId>

        <version>${activiti.version}</version>

    </dependency>

    <dependency>

         <groupId>org.activiti</groupId>

         <artifactId>activiti-json-converter</artifactId>

         <version>${activiti.version}</version>

         <exclusions>

         <exclusion>

         <artifactId>commons-collections</artifactId>

         <groupId>commons-collections</groupId>

         </exclusion>

         </exclusions>

        </dependency>

    <!-- activiti end -->

    <!-- LOGGING begin -->

    <dependency>

    <groupId>org.slf4j</groupId>

    <artifactId>slf4j-api</artifactId>

    <version>${slf4j.version}</version>

    </dependency>

    <dependency>

    <groupId>org.slf4j</groupId>

    <artifactId>slf4j-log4j12</artifactId>

    <version>${slf4j.version}</version>

    </dependency>

    <dependency>

    <groupId>org.slf4j</groupId>

    <artifactId>jcl-over-slf4j</artifactId>

    <version>${slf4j.version}</version>

    </dependency>

    <dependency>

    <groupId>org.slf4j</groupId>

    <artifactId>jul-to-slf4j</artifactId>

    <version>${slf4j.version}</version>

    </dependency>

    <!-- LOGGING end -->

    <!-- jackson json -->

    <dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-core</artifactId>

    <version>${jackson.version}</version>

    </dependency>

    <dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-databind</artifactId>

    <version>${jackson.version}</version>

    </dependency>

    <dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-annotations</artifactId>

    <version>${jackson.version}</version>

    </dependency>

    <dependency>

    <groupId>com.fasterxml.jackson.module</groupId>

    <artifactId>jackson-module-jaxb-annotations</artifactId>

    <version>${jackson.version}</version>

    </dependency>

        <dependency>

          <groupId>junit</groupId>

          <artifactId>junit</artifactId>

          <version>4.11</version>

          <scope>test</scope>

        </dependency>

      </dependencies>

      <build>

        <finalName>activiti-web-modeler</finalName>

      </build>

    </project>

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

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

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="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">

    <description>Spring Configuration</description>

    <context:property-placeholder ignore-unresolvable="true" location="classpath:activiti.properties" />

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

    <property name="dataSource" ref="dataSource" />

    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

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

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

    <property name="url" value="${jdbc.url}" />

    <property name="username" value="${jdbc.username}" />

    <property name="password" value="${jdbc.password}" />

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

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

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

    <property name="maxWait" value="60000" />

    <property name="timeBetweenEvictionRunsMillis" value="60000" />

    <property name="minEvictableIdleTimeMillis" value="300000" />

    <property name="testWhileIdle" value="true" />

    <property name="testOnBorrow" value="false" />

    <property name="testOnReturn" value="false" />

    </bean>

    <context:component-scan base-package="org.activiti.conf,org.activiti.rest.editor,org.activiti.rest.service">

            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

        </context:component-scan>

        <bean id="restResponseFactory" class="org.activiti.rest.service.api.RestResponseFactory"></bean>

    <!-- json处理--> 

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

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

    <!-- Activiti begin -->

    <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">

    <property name="dataSource" ref="dataSource" />

    <property name="transactionManager" ref="transactionManager" />

    <property name="databaseSchemaUpdate" value="true" />

    <property name="jobExecutorActivate" value="true" />

    <property name="history" value="full" />

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

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

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

    </bean>

    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">

    <property name="processEngineConfiguration" ref="processEngineConfiguration" />

    </bean>

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

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

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

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

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

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

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

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

    </beans>

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

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="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">

        <description>Spring MVC Configuration</description>

        <context:component-scan base-package="org.activiti.rest.editor,org.activiti.rest.diagram">

            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />

        </context:component-scan>

        <context:component-scan base-package="org.tzxx.controller">

        </context:component-scan>

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

        <mvc:annotation-driven />

        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property name="prefix" value="/WEB-INF/jsp/" />

    <property name="suffix" value=".jsp" />

    </bean>

    </beans>

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

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="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">

        <description>Spring MVC Configuration</description>

        <context:component-scan base-package="org.activiti.rest">

            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />

        </context:component-scan>

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

        <mvc:annotation-driven />

    </beans>

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

    (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文件来实例化上下文信息

    <!DOCTYPE web-app PUBLIC

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

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

    <web-app>

      <display-name>Archetype Created Web Application</display-name>

      <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath:beans.xml</param-value>

      </context-param>

        <listener>

           <listener-class>

          org.springframework.web.context.ContextLoaderListener

       </listener-class>

       </listener>

       <listener>

           <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>

       </listener>

        <filter>

            <filter-name>encodingFilter</filter-name>

            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

            <init-param>

                <param-name>encoding</param-name>

                <param-value>UTF-8</param-value>

            </init-param>

            <init-param>

                <param-name>forceEncoding</param-name>

                <param-value>true</param-value>

            </init-param>

        </filter>

        <filter-mapping>

            <filter-name>encodingFilter</filter-name>

            <url-pattern>/*</url-pattern>

        </filter-mapping>

      <!-- springmvc的servlet -->

      <servlet>

        <servlet-name>ModelRestServlet</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>classpath:spring-mvc.xml</param-value>

        </init-param>

        <load-on-startup>1</load-on-startup>

      </servlet>

      <servlet-mapping>

        <servlet-name>ModelRestServlet</servlet-name>

        <url-pattern>/service/*</url-pattern>

      </servlet-mapping>

      <servlet>

        <servlet-name>RestServlet</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>classpath:spring-rest.xml</param-value>

        </init-param>

        <load-on-startup>1</load-on-startup>

      </servlet>

      <servlet-mapping>

        <servlet-name>RestServlet</servlet-name>

        <url-pattern>/rest/*</url-pattern>

      </servlet-mapping>

    </web-app>

    (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<Model> 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接口的搭建

          本文链接:https://www.haomeiwen.com/subject/lbiahctx.html