spring boot 整合activiti7及 activiti model

前言

前面用spring boot整合了activiti5 ,这次做一个升级一步步记录如何整合及整合过程中遇到的问题,希望大家遇到类似的问题后 能快速解决

一、下载官网的demo代码

https://github.com/Activiti/activiti-examples
下载官网的demo,下载下来后,我切到了7.0.x版本的分支,结构如下:

spring boot 整合activiti7及 activiti model_第1张图片
下载下来的代码有很多报错,因为jar包找不到,打开任何一个子模块,最下面有一个仓库地址,activiti 7 的jar依赖需要从这个仓库地址下载

<repositories>
    <repository>
      <id>alfrescoid>
      <name>Activiti Releasesname>
      <url>https://artifacts.alfresco.com/nexus/content/repositories/activiti-releases/url>
      <releases>
        <enabled>trueenabled>
      releases>
    repository>
  repositories>

二、代码整合

jar下载下来后,不报错后,随意选一个模块,进行整合,我选 了activiti-api-spring-integration-example这个模块
注解其它类,只保留 DemoApplication这个启动类,并只保留了main方法

1、添加yml配置

# 服务配置
server:
  port: 8085

# Spring相关配置
spring:
  ##数据库连接信息
  datasource:
    # 数据源配置
    type: com.zaxxer.hikari.HikariDataSource
    url: jdbc:mysql://127.0.0.1:3309/activity?useUnicode=true&characterEncoding=utf8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&useSSL=false&allowMultiQueries=true&nullCatalogMeansCurrent=true
    username: root
    password: 123456

    # SQLException: XAER_INVAL: Invalid arguments (or unsupported command)问题
    xa:
      properties:
        pinGlobalTxToPhysicalConnection: true
        useServerPrepStmts: true

  thymeleaf:
    mode: HTML
    encoding: utf-8
    # 禁用缓存
    cache: false
  application:
    # 注册应用名
    name: actdemo
  mvc:
    # 静态资源路径
    static-path-pattern: /static/**
  # 参考配置https://www.cnblogs.com/liaojie970/p/8857710.html
  activiti:
    # 自动建表
    database-schema: activity
    database-schema-update: true
    history-level: full
    db-history-used: true

2、添加依赖

我用的Myslq,所以添加Mysql依赖,其中一些依赖是用于整合官方编辑器用的


    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-thymeleafartifactId>
    dependency>

    
    <dependency>
      <groupId>org.activitigroupId>
      <artifactId>activiti-image-generatorartifactId>
    dependency>

    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-jdbcartifactId>
    dependency>
    

    <dependency>
      <groupId>org.activitigroupId>
      <artifactId>activiti-json-converterartifactId>
      <version>6.0.0version>
    dependency>

    <dependency>
      <groupId>org.projectlombokgroupId>
      <artifactId>lombokartifactId>
      <scope>providedscope>
      <version>1.16.18version>
    dependency>



    
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
    dependency>

    <dependency>
      <groupId>com.github.pagehelpergroupId>
      <artifactId>pagehelper-spring-boot-starterartifactId>
      <version>1.2.10version>
    dependency>


    <dependency>
      <groupId>org.mybatis.spring.bootgroupId>
      <artifactId>mybatis-spring-boot-starterartifactId>
      <version>1.3.2version>
    dependency>



    
    
    <dependency>
      <groupId>org.apache.xmlgraphicsgroupId>
      <artifactId>batik-svg-domartifactId>
      <version>1.7version>
      <scope>compilescope>
      <exclusions>
        <exclusion>
          <groupId>org.apache.xmlgraphicsgroupId>
          <artifactId>batik-domartifactId>
        exclusion>
        <exclusion>
          <groupId>org.apache.xmlgraphicsgroupId>
          <artifactId>batik-animartifactId>
        exclusion>
      exclusions>
    dependency>

    <dependency>
      <groupId>org.apache.xmlgraphicsgroupId>
      <artifactId>batik-domartifactId>
      <version>1.7version>
    dependency>

    <dependency>
      <groupId>org.apache.xmlgraphicsgroupId>
      <artifactId>batik-svggenartifactId>
      <version>1.7version>
    dependency>
    <dependency>
      <groupId>org.apache.xmlgraphicsgroupId>
      <artifactId>batik-awt-utilartifactId>
      <version>1.7version>
    dependency>
    <dependency>
      <groupId>org.apache.xmlgraphicsgroupId>
      <artifactId>batik-parserartifactId>
      <scope>compilescope>
      <version>1.7version>
    dependency>

    <dependency>
      <groupId>org.apache.xmlgraphicsgroupId>
      <artifactId>batik-codecartifactId>
      <version>1.7version>
    dependency>
    

    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>fastjsonartifactId>
      <version>1.2.58version>
    dependency>


    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-devtoolsartifactId>
    dependency>

    <dependency>
      <groupId>net.sf.dozergroupId>
      <artifactId>dozerartifactId>
      <version>5.5.1version>
    dependency>


3、添加官方编辑器

activit后面的源码下载后,已经找不到编辑器了,所以我用了之前的代码

3.1、添加几个主要的类

spring boot 整合activiti7及 activiti model_第2张图片

3.2、添加js

spring boot 整合activiti7及 activiti model_第3张图片

3.3、访问列表方法http://localhost:8085/models/modelist,显示登录页面

spring boot 整合activiti7及 activiti model_第4张图片
activity7 会在内存中生成一个用户名,密码。
用户名固定是 : user
密码是随机生成UUID,密码生成后,会在控制台打印出现如下:

Using generated security password: 18cc84ba-b9ae-473f-92a9-d640f13136d0

ctrl+F ,查找关键字Using generated security password

3.4、输入用户名密码后,可以显示列表面如下:

在这里插入图片描述

/**
     * 获取所有模型
     * @return
     */
    @GetMapping
    @RequestMapping("modelist")
    public String modelList(org.springframework.ui.Model model){
        securityUtil.logInAs("system");
        List<Model> models = repositoryService.createModelQuery().orderByCreateTime().desc().list();
        model.addAttribute("models",models);
        return "model/list";
    }

3.5、点击创建新建按钮,还是会调用后台一个方法:

/**
     * 新建一个空模型
     * @return
     * @throws UnsupportedEncodingException
     */
    @PostMapping("newmodel")
    public Object newModel(NewModel newModel) throws UnsupportedEncodingException {
        //securityUtil.logInAs("admin");
        //初始化一个空模型
        Model model = repositoryService.newModel();

        //设置一些默认信息
        String name = newModel.getName();
        String description = newModel.getDes();
        int revision = 1;
        String key = newModel.getKey();

        ObjectNode modelNode = objectMapper.createObjectNode();
        modelNode.put(ModelDataJsonConstants.MODEL_NAME, name);
        modelNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
        modelNode.put(ModelDataJsonConstants.MODEL_REVISION, revision);

        model.setName(name);
        model.setKey(key);
        model.setMetaInfo(modelNode.toString());

        repositoryService.saveModel(model);
        String id = model.getId();

        //完善ModelEditorSource
        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);
        repositoryService.addModelEditorSource(id,editorNode.toString().getBytes("utf-8"));
        return "redirect:/static/modeler.html?modelId="+id;
    }

但是会报这个403, 貌似 是crsf
spring boot 整合activiti7及 activiti model_第5张图片

3.6、关闭csrf

@Component
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.csrf().disable();
    }
}

再次访问列表页,用户名和密码也不用再输入了。

4、创建一个流程

spring boot 整合activiti7及 activiti model_第6张图片
spring boot 整合activiti7及 activiti model_第7张图片

三、遇到问题

1、jar下载失败

切到demo中pom的仓库,下载jar

<repositories>
    <repository>
      <id>alfresco</id>
      <name>Activiti Releases</name>
      <url>https://artifacts.alfresco.com/nexus/content/repositories/activiti-releases/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
    </repository>
  </repositories>

2、刚开始启动时,不创建表

数据库url中添加 &nullCatalogMeansCurrent=true

3、从activity5 中 复制的3个Java类,报错,引入相应的Jar包即可

其中继承了一个静态接口的 类,去掉静态接口继承,直接将静态接口中 定义的几个常量 复制出来,自己创建一个接口即可,再重定引入,如下

public interface  ModelDataJsonConstants {
    String MODEL_ID = "modelId";
    String MODEL_NAME = "name";
    String MODEL_REVISION = "revision";
    String MODEL_DESCRIPTION = "description";
}

4、需要输入用户名、密码

可以自己在内存中定义或 直接关闭crfs。
在内存定义,实际demo原本就有,现在放开注释掉的代码:

/**
     * 如果不注入这个bean,那么会在内存中生一个固定用户user , 密码为UUID
     * 密码会在控制台 打印出来,关键字:Using generated security password
     * @return
     */

    @Bean
    public UserDetailsService myUserDetailsService() {

        InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();

        String[][] usersGroupsAndRoles = {
                //定义了2个用户名,密码
                {"system", "password", "ROLE_ACTIVITI_USER"},
                {"admin", "password", "ROLE_ACTIVITI_ADMIN"},
        };

        for (String[] user : usersGroupsAndRoles) {
            List authoritiesStrings = Arrays.asList(Arrays.copyOfRange(user, 2, user.length));
            logger.info("> Registering new user: " + user[0] + " with the following Authorities[" + authoritiesStrings + "]");
            inMemoryUserDetailsManager.createUser(new User(user[0], passwordEncoder().encode(user[1]),
                    authoritiesStrings.stream().map(s -> new SimpleGrantedAuthority(s)).collect(Collectors.toList())));
        }


        return inMemoryUserDetailsManager;

    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

5、访问新建模块接口 403

关闭crfs

6、这一版的插件引入写的没有那么详细,可以参见

spring boot(十一) 整合Activiti工作流(三)官方编辑器插件Activiti Model

你可能感兴趣的:(spring,boot,spring,boot,activiti)