spring boot整合activiti rest api详细教程

1.导入依赖

         <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-rest</artifactId>
            <version>${activiti.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-common-rest</artifactId>
            <optional>true</optional>
            <version>${activiti.version}</version>
        </dependency>

推荐使用如下方式

    <dependency>
    <groupId>org.activiti</groupId>
    <artifactId>activiti-spring-boot-starter-basic</artifactId>
    <version>${activiti.version}</version>
    </dependency>
   <dependency>
   <groupId>org.activiti</groupId>
   <artifactId>activiti-spring-boot-starter-rest-api</artifactId>
   <version>${activiti.version}</version>
    </dependency>

 

2.扫描activiti rest controller所在的包

图片.png

扫描org.activiti.rest就行了,会扫描下面的所有子包。

@SpringBootApplication
@ComponentScan("org.activiti.rest")
public class SpringBootWithActivitiApplication {
···

3.配置两个bean

    //rest service
    @Bean
    public RestResponseFactory restResponseFactory(){
        return new RestResponseFactory();
    }
    @Bean
    public ContentTypeResolver contentTypeResolver(){
        return new DefaultContentTypeResolver();
    }

4.使用rest api

如:
1.获取所有deployments:

GET 
/repository/deployments

response:

{
  "data": [
    {
      "id": "10",
      "name": "activiti-examples.bar",
      "deploymentTime": "2010-10-13T14:54:26.750+02:00",
      "category": "examples",
      "url": "http://localhost:8081/service/repository/deployments/10",
      "tenantId": null
    }
  ],
  "total": 1,
  "start": 0,
  "sort": "id",
  "order": "asc",
  "size": 1
}

2.根据id查询deployment

GET
/repository/deployments/{deploymentId}

response:

{
  "id": "10",
  "name": "activiti-examples.bar",
  "deploymentTime": "2010-10-13T14:54:26.750+02:00",
  "category": "examples",
  "url": "http://localhost:8081/service/repository/deployments/10",
  "tenantId" : null
}

3.根据id删除deployment

DELETE
/repository/deployments/{deploymentId}

response:
成功时,状态码为204。
4.更多的activiti rest api
https://www.activiti.org/userguide/#_rest_api

5、由于做个单独服务,activiti-rest会默认进行basic authentication,那么就需要屏蔽验证。

 在boot服务启动类里面加上这个,boot启动的时候就会屏幕,rest-client请求的时候就不会验证了

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

配置config scan扫描activiti-rest 的controller 接口信息

@Configuration
@ComponentScan(basePackages = { "org.activiti.rest.diagram", "org.activiti.rest"}, includeFilters = {
        @Filter(type = FilterType.ANNOTATION, classes = {
                RestController.class }) }, useDefaultFilters = false, lazyInit = false)
public class ScanConfig {

}

配置数据源信息 以及要扫描注入的activiti bean
 

/*
 * ApplicationContext ac1 =
 * WebApplicationContextUtils.getRequiredWebApplicationContext(
 * ServletContext sc); /* ApplicationContext ac2 =
 * WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
 * /* ac1.getBean("beanId"); /* ac2.getBean("beanId");
 */
@Bean(name = "ProcessEngineConfiguration")
public ProcessEngineConfiguration getProcess(@Qualifier("flowdataDataSource") DataSource dataSource) {
    ProcessEngineConfiguration configuration = ProcessEngineConfiguration
            .createStandaloneProcessEngineConfiguration();
    configuration.setDataSource(dataSource);
    configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
    configuration.buildProcessEngine();
    configuration.setActivityFontName("宋体");
    configuration.setLabelFontName("宋体");
    return configuration;
}

@Bean(name = "restResponseFactory")
public RestResponseFactory restResponseFactory() {
    return new RestResponseFactory();
}

@Bean(name = "contentTypeResolver")
public ContentTypeResolver contentTypeResolver() {
    return new DefaultContentTypeResolver();
}

@Bean(name = "processEngineConfiguration")
public ProcessEngineConfiguration processEngineConfiguration() {
    return processEngineConfiguration;
}


接下来配置七大接口数据信息


@Configuration
public class ActivitiServiceConfig {

    @Bean
    public StandaloneProcessEngineConfiguration getStandaloneProcessEngineConfiguration() {
        StandaloneProcessEngineConfiguration standaloneProcessEngineConfiguration = new StandaloneProcessEngineConfiguration();
        return standaloneProcessEngineConfiguration;
    }
    @Bean(name = "processEngine") // processEngineConfiguration
    public ProcessEngine getProsessEngien(@Qualifier("ProcessEngineConfiguration") ProcessEngineConfiguration pro) {
        return pro.buildProcessEngine();
    }

    @Bean(name = "RepositoryService")
    public RepositoryService getRepositoryService(@Qualifier("processEngine") ProcessEngine ProcessEngine) {
        return ProcessEngine.getRepositoryService();
    }

    @Bean(name = "RuntimeService")
    public RuntimeService getRuntimeService(@Qualifier("processEngine") ProcessEngine ProcessEngine) {
        return ProcessEngine.getRuntimeService();
    }

    @Bean(name = "TaskService")
    public TaskService getTaskService(@Qualifier("processEngine") ProcessEngine ProcessEngine) {
        return ProcessEngine.getTaskService();
    }

    @Bean(name = "FormService")
    public FormService getFormService(@Qualifier("processEngine") ProcessEngine ProcessEngine) {
        return ProcessEngine.getFormService();
    }

    @Bean(name = "HistoryService")
    public HistoryService getHistoryService(@Qualifier("processEngine") ProcessEngine ProcessEngine) {
        return ProcessEngine.getHistoryService();
    }

    @Bean(name = "ManagementService")
    public ManagementService getManagementService(@Qualifier("processEngine") ProcessEngine ProcessEngine) {
        return ProcessEngine.getManagementService();
    }

    @Bean(name = "IdentityService")
    public IdentityService getIdentityService(@Qualifier("processEngine") ProcessEngine ProcessEngine) {
        return ProcessEngine.getIdentityService();
    }
}


启动项目

package com;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
//import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@EnableHystrixDashboard
@EnableCircuitBreaker
// @EnableEurekaClient
//@EnableAutoConfiguration
@EnableDiscoveryClient
@RestController
@SpringBootApplication
@EnableTransactionManagement
// @EnableScheduling
@EnableAutoConfiguration(exclude = { org.activiti.rest.security.BasicAuthenticationProvider.class,
        org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class,
        org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration.class,
        org.activiti.rest.security.BasicAuthenticationProvider.class })
/*
 * @ComponentScan(basePackages = { "org.activiti.rest.diagram",
 * "org.activiti.rest" }, includeFilters = {
 * 
 * @Filter(type = FilterType.ANNOTATION, classes = { RestController.class }) },
 * useDefaultFilters = false, lazyInit = false)
 */
// <mvc:annotation-driven />
public class App {// implements CommandLineRunner {

    @RequestMapping("/")
    public String index() {
        return "hello world at home!";
    }
    public static void main(String[] args) {
        // SpringApplication.run(App.class, args);
        new SpringApplicationBuilder(App.class).web(true).run(args);
    }
}



启动项目截图 我们发现activiti-rest 的controller 已经加载进来了 
 

springboot无法在其他表空间创建25张表,使用加载activiti.cfg.xml的方式进行创建表。

<beans xmlns="http://www.springframework.org/schema/beans"   
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans.xsd">  
  
       <!-- Activiti处理引擎的配置,交由Spring管理 -->  
  
         <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">  
          <!-- 配置数据源,和系统使用同一个数据源 -->  
          <property name="dataSource" ref="dataSource" />  
          <property name="databaseSchemaUpdate" value="true" />  
          <property name="jobExecutorActivate" value="false" />  
          <!-- 统一的事务管理 -->  
          <property name="transactionManager" ref="transactionManager" />  
          <property name="activityFontName"  value="宋体" />  
          <property name="labelFontName"  value="宋体" />  
          <property name="databaseSchema" value="ACT"/>
         </bean>  
  
       <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">  
          <property name="processEngineConfiguration" ref="processEngineConfiguration" />  
       </bean> 
       <!-- 
       <bean id="testService2" class="com.imm.amo.controller.ActivitiTest"></bean> 
       -->
</beans>  
package com.imm.config;
 
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
 
 
@Configuration
@ImportResource(locations={"classpath:activiti.cfg.xml"})
public class config {
 
}


启动项目,创建activiti表。
但是因为使用了xml方式加载activiti,所以无法自动发布流程定义。固加载成功之后,去除@ImportResource即可。

删除activiti表时,注意要删除SEQUENCE ACT_EVT_LOG_SEQ。
============================================================

Spring MVC集成

1.web工程已经集成进入activiti5.22版本。

2.引入jar包。

<dependency>
    <groupId>org.activiti</groupId>
    <artifactId>activiti-rest</artifactId>
    <version>5.22.0</version>
</dependency>
3.web.xml文件加入
<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:conf/spring/spring-mvc-rest.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>RestServlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>


4.
spring-mvc-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-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <!-- 自动扫描且只扫描@Controller -->
    <context:component-scan base-package="org.activiti.rest">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <mvc:annotation-driven />
</beans>


5.activiti5.22的配置文件中加入如下:   

 <!-- 加载配置属性文件 -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:application.properties" />

    <!-- Activiti begin -->
    <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
        <property name="dataSource" ref="dataSource" />
        <property name="transactionManager" ref="transactionManager" />
        <property name="deploymentResources" value="classpath*:/activiti/*.zip"/>
        <property name="databaseSchemaUpdate" value="true" /><!-- 自动建表,自动更新表结构 -->
        <property name="jobExecutorActivate" value="false" /><!-- 该参数将启用定时相关的功能 -->
        <property name="history" value="full" /><!-- 历史记录保存模式 -->

        <!-- UUID作为主键生成策略
        <property name="idGenerator" ref="idGen" />-->

        <!-- 生成流程图的字体 -->
        <property name="activityFontName" value="${activiti.diagram.activityFontName}"/>
        <property name="labelFontName" value="${activiti.diagram.labelFontName}"/>
        <property name="annotationFontName" value="${activiti.diagram.annotationFontName}"/><!-- 5.21.0 新增参数 -->

        <!-- 自定义用户权限 ,必须重新实现用户和组的相应方法
        <property name="customSessionFactories">
            <list>
                <bean class="com.thinkgem.jeesite.modules.act.service.ext.ActUserEntityServiceFactory"/>
                <bean class="com.thinkgem.jeesite.modules.act.service.ext.ActGroupEntityServiceFactory"/>
            </list>
        </property>-->
    </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="restResponseFactory" class="org.activiti.rest.service.api.RestResponseFactory"/>
    <!-- json处理-->
    <bean id="objectMapper" class="com.fasterxml.jackson.databind.ObjectMapper" />
    <!-- 设置默认的资源类型 -->
    <bean id="contentTypeResolver" class="org.activiti.rest.common.application.DefaultContentTypeResolver" />

    <!-- Activiti end -->

 

1.根据快速开始操作,创建maven工程,配置相关依赖, $actVer改为6.0.0 本例使用mysql替代h2

<dependency>
    <groupId>org.activiti</groupId>
    <artifactId>activiti-engine</artifactId>
    <version>6.0.0</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.21</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.21</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.6</version>
</dependency>

2.加入配置文件/src/main/resources/log4j.properties

log4j.rootLogger=DEBUG, ACT
log4j.appender.ACT=org.apache.log4j.ConsoleAppender
log4j.appender.ACT.layout=org.apache.log4j.PatternLayout
log4j.appender.ACT.layout.ConversionPattern= %d{hh:mm:ss,SSS} [%t] %-5p %c %x - %m%n


3.mysql创建数据库并连接 


4.获取流程引擎对象ProcessEngine 
4.1.通过代码设置

// 引擎配置
ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
        .setJdbcUrl("jdbc:mysql://localhost:3306/activiti").setJdbcUsername("root")
        .setJdbcPassword("pwd")
        .setJdbcDriver("com.mysql.jdbc.Driver")
        .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
// 获取流程引擎对象
ProcessEngine processEngine = cfg.buildProcessEngine();


4.2.通过activiti.cfg.xml配置文件

ProcessEngineConfiguration cfg = ProcessEngineConfiguration
        .createProcessEngineConfigurationFromResource("activiti.cfg.xml");
ProcessEngine processEngine = cfg.buildProcessEngine();


配置文件activiti.cfg.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti"/>
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUsername" value="root"/>
        <property name="jdbcPassword" value="pwd"/>
        <property name="databaseSchemaUpdate" value="true"/>
    </bean>

</beans>



执行后初始化数据表如下 
 
5.安装BPM插件 
5.1 IDEA搜索并安装BPM插件 Business Process Management

加入示例中的xml /src/main/resources/onboarding.bpmn20.xml 
打开xml可以看到流程图 

5.2.IDEA的BPM插件使用体检较差,建议使用eclipse插件 
安装插件 Install New Software http://activiti.org/designer/update/ 
Activiti 5.10支持直接部署扩展名为bpmn的流程文件,设计器打开如下: 

下载 
https://www.activiti.org/download-links 
快速开始 
https://www.activiti.org/quick-start 
用户指南 
https://www.activiti.org/userguide/ 
相关资料 
http://www.kafeitu.me/activiti.html 
http://blog.csdn.net/u013517797/article/category/6732665

posted @ 2018-11-20 17:07  柚子=_=  阅读(2315)  评论(1编辑  收藏  举报