“This is the 10th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Author: Tangyuan

Personal blog: Javalover.cc

preface

In the previous article, we learned a starting example based on JavaApi. Parameters are mainly passed through the command line.

In this article, learn an example of how to get started with the REST API;

Here we will learn how to use existing REST examples, since the official war package is already available for us to use

The process used in it is basically the same as that in the previous article, as shown in the following table of contents:

directory

  1. Download the flowable – rest. War file
  2. Start the Flowable – Rest application
  3. Deploy a process definition
  4. Start process instance
  5. Get the task list
  6. Querying Historical Records

The body of the

1. Download the Flowable-rest. war package

Download address:

Flowable -rest.war is in the wars directory

2. Start the Flowable rest application

Go to the wars directory and start from the command line: Java -jar flowable -restat.war

After startup, the following information is displayed:

When the last line, according to the INFO [main] org. Apache. Catalina. Startup. Catalina. Start Server startup in xyz, ms started successfully

At this point we can confirm with a simple request:

curl --user rest-admin:test http://localhost:8080/flowable-rest/service/management/engine
Copy the code

All requests are authenticated in basic authentication mode

User name/password: reset-admin/test

If the JSON object containing the flowable version is returned, the startup is successful

3. Define the deployment process

The following start, basically follow the steps of a consistent, is the request way is different;

The last article was directly in the Java program, based on Java API;

Here we need to upload the process definition file bPMn.xml to the database as follows: Request format is multipart/ formData

curl --user rest-admin:test -F "[email protected]" http://localhost:8080/flowable-rest/service/repository/deployments
Copy the code

Let’s get the list of process definitions to view the process definition we just uploaded:

curl --user rest-admin:test http://localhost:8080/flowable-rest/service/repository/process-definitions
Copy the code

One of the returned lists with a holidayRequest key is the process definition we just uploaded

The process ID =”holidayRequest” in our process definition corresponds to the key here

4. Start the process instance

Start, need to set up a good process variables;

This time we will pass the JSON object directly, as shown below:

curl --user rest-admin:test -H "Content-Type: application/json" -X POST -d '{ "processDefinitionKey":"holidayRequest", "variables": [ { "name":"employee", "value": "John Doe" }, { "name":"nrOfHolidays", "value": 7 }]}' http://localhost:8080/flowable-rest/service/runtime/process-instances
Copy the code

5. Obtain the task list

Here we query the task list belonging to the manager

curl --user rest-admin:test -H "Content-Type: application/json" -X POST -d '{ "candidateGroup" : "managers" }' http://localhost:8080/flowable-rest/service/query/tasks
Copy the code

The tasks queried here are the following user tasks in the process definition:

<userTask id="approveTask" name="Approve or reject request" flowable:candidateGroups="managers"/>

6. Perform user tasks

Now we can call the interface to perform the user task;

curl --user rest-admin:test -H "Content-Type: application/json" -X POST -d '{ "action" : "complete", "variables" : [ { "name" : "approved", "value" : true} ] }' http://localhost:8080/flowable-rest/service/runtime/tasks/25
Copy the code

The parameter after tasks in the above request URL is the task ID, which is returned when the task list is obtained

After execution, an error is reported, as shown in the figure above, indicating that the class cannot be found.

This class is the one executed after the request is granted, and is configured in bPMn.xml as follows:

<serviceTask id="externalSystemCall" name="Enter holidays in external system"
                     flowable:class="org.flowable.CallExternalSystemDelegate"/>
Copy the code

Here are the missing class source code: CallExternalSystemDelegate. Java

package org.flowable;

import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;

public class CallExternalSystemDelegate implements JavaDelegate {

    public void execute(DelegateExecution execution) {
        System.out.println("Calling the external system for employee "
                + execution.getVariable("employee")); }}Copy the code

So how do you add it?

Instead of following the official tutorial to package the missing classes and deploy them to lib (because of all sorts of weird errors), here we go.

Instead, deploy the class file directly into the WAR project as follows:

  1. Unpack the flowable – rest. War:jar xf .\flowable-rest.war
  2. Add the missing class to org/flowable
  3. Delete the flowable – rest. War:rm .\flowable-rest.war(Backup before deleting)
  4. Repackage as jar:jar cf0M flowable-rest.jar *
  5. Activation:java -jar flowable-rest.jar

The interface returns 200, but no value is returned.

However, logs are displayed in the background, as follows:

Here my name is in Chinese (Tangyuan learning Java), so the front is not printed, this is not important;

This completes a REST API-based request flow;

For more information about the API, see the REST API website

conclusion

This article mainly introduces the API to call flowable through Rest style. The core process is the same as the previous article.