springboot integrates activiti with modeler for process creation, editing, deployment and deletion of instances (runnable)

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter-basic</artifactId>
            <version>5.22.0</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-diagram-rest</artifactId>
            <!-- exclusions: activiti-diagram-rest contains a security jar package, which leads to the need to log in to access springbott, so the login is removed here -->
            <exclusions>
                <exclusion>
                    <artifactId>spring-security-config</artifactId>
                    <groupId>org.springframework.security</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-security-core</artifactId>
                    <groupId>org.springframework.security</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-security-crypto</artifactId>
                    <groupId>org.springframework.security</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-security-web</artifactId>
                    <groupId>org.springframework.security</groupId>
                </exclusion>
            </exclusions>
            <version>5.22.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-common-rest</artifactId>
            <version>5.19.0</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-json-converter</artifactId>
            <version>5.22.0</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-modeler</artifactId>
            <exclusions>
            <exclusion>
                <artifactId>spring-security-config</artifactId>
                <groupId>org.springframework.security</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-security-core</artifactId>
                <groupId>org.springframework.security</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-security-crypto</artifactId>
                <groupId>org.springframework.security</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-security-web</artifactId>
                <groupId>org.springframework.security</groupId>
            </exclusion>
            </exclusions>
            <version>5.19.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/batik/batik-script -->
    </dependencies>

1. Now let’s talk about the process, first create a spring boot project, and import the corresponding jar package.

2. Obtain these static resource files from the officially released activiti-explorer.war package and copy them to the corresponding directory, as follows:

Then copy a few encapsulated background logics (save process templates and other operations);

2. After copying, add @RequestMapping(“/service”) path restrictions to the three background files to avoid conflicts with the jar’s controller path:

Modify the resources\static\editor-app\app-cfg.js file and specify it as the access path of the three files we copied just now

Among them, the stencilset.json file is the interface control configuration of our editor, which can be modified by yourself. You can add or reduce the controls and properties you need in your actual business.

3. Set up our database link configuration at this point:

erver.port=8888
spring.datasource.url=jdbc:mysql://localhost:3306/activitiDemo?characterEncoding=UTF-8 &nullCatalogMeansCurrent=true &serverTimezone=GMT &useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##Every time the application starts, do not check whether the Activiti data table exists and whether the version number matches. The first time it is set to true, the table structure will be created automatically, and then it can be set to false to improve the running speed
spring.activiti.database-schema-update=true

4. Create maincontroller and open our access interface entry:

@Autowired
    private RepositoryService repositoryService;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private FormService formService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private ProcessEngineConfiguration processEngineConfiguration;
 /**
     * create model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/createModel")
    public String createModel(HttpServletRequest request, HttpServletResponse response) {
        String name = "Leave Request Process";
        String description = "This is a leave process";
        String id = null;
        try {
            Model model = repositoryService. newModel();
            String key = name;
            //version number
            String revision = "1";
            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 classification combined with its own business logic
            //model.setCategory(category);
            model.setMetaInfo(modelNode.toString());
            repositoryService. saveModel(model);
            id = model. getId();
            //Improve 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"));
            String aa = request. getContextPath();
            response.sendRedirect(request.getContextPath() + "/modeler.html?modelId=" + id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "index";
    }

Finally start the project: Visit http://localhost:8888/createModel to enter the editor home page at the beginning of the article

5. It must be saved after creation. This uses the ModelSaveRestResource class that comes with the framework. It should be noted here that when actually saving, it is likely to report error 500 or 403, indicating that the parameters do not match. This is a pit I stepped on myself. I need to modify it by myself and expand the parameters. If there is no problem, you can ignore this step:

 public void saveModel(@PathVariable String modelId, String name, String description, String json_xml,
                        String svg_xml) { }

6. After the basic process can run, the above mentioned already includes the use of modeler to create and save the instance, and then there are steps such as deployment, deletion, opening, process tracking, etc., because many of the Internet are unit test instances, yes Beginners are very unfriendly, so I integrated some simple interfaces to demonstrate these processes. Because the code is too complicated, I will directly explain the usage process and paste the code:

Specifically, you have to operate it yourself, the code will be pasted out, and you can visit http://localhost:8888/index by yourself. Activiti’s database, if there is no table, will automatically create a table. The description of the table can be Baidu by yourself. There are quite a lot of articles in this part.