You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何用ProcessMaker 4.0 API开发Java应用及API调用示例

PM4 RESTful API 认证与调用示例(Java)

认证说明

PM4的REST API采用OAuth2协议做身份验证,既然你提到"Auth client"模块里没有"Enable direct API access"选项,可以用密码授权模式(Resource Owner Password Credentials)获取访问令牌,这种方式适合后台服务直接通过用户名密码获取权限调用API。

Java 示例代码

以下是基于Spring RestTemplate的完整示例,涵盖令牌获取、流程数据查询与更新的基础操作:

1. 依赖准备(Maven)

如果是Spring项目,确保pom.xml包含以下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>

2. 核心代码实现

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class PM4ApiClient {

    // 替换为你的PM4 docker部署地址
    private static final String PM4_BASE_URL = "http://localhost:8080";
    // OAuth2令牌获取端点
    private static final String TOKEN_URL = PM4_BASE_URL + "/oauth/token";
    // 流程数据操作端点(根据实际需求替换)
    private static final String PROCESS_DATA_URL = PM4_BASE_URL + "/api/process-instances/{instanceId}/data";

    // 替换为你的PM4登录账号密码
    private static final String USERNAME = "admin";
    private static final String PASSWORD = "admin";
    // PM4默认客户端ID,可在后台Auth Clients模块确认
    private static final String CLIENT_ID = "pm4-client";
    private static final String CLIENT_SECRET = "";

    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();

        // 1. 获取访问令牌
        String accessToken = getAccessToken(restTemplate);
        if (accessToken == null) {
            System.err.println("令牌获取失败");
            return;
        }

        // 2. 查询指定流程实例的流程数据
        String instanceId = "YOUR_PROCESS_INSTANCE_ID"; // 替换为实际流程实例ID
        String processData = getProcessData(restTemplate, accessToken, instanceId);
        System.out.println("当前流程数据:" + processData);

        // 3. 更新流程数据
        String updatedDataJson = "{\"yourKey\": \"newValue\"}"; // 替换为实际更新的JSON内容
        boolean updateResult = updateProcessData(restTemplate, accessToken, instanceId, updatedDataJson);
        System.out.println("流程数据更新:" + (updateResult ? "成功" : "失败"));
    }

    private static String getAccessToken(RestTemplate restTemplate) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
        requestBody.add("grant_type", "password");
        requestBody.add("username", USERNAME);
        requestBody.add("password", PASSWORD);
        requestBody.add("client_id", CLIENT_ID);

        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(requestBody, headers);

        try {
            ResponseEntity<TokenResponse> response = restTemplate.postForEntity(TOKEN_URL, request, TokenResponse.class);
            if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) {
                return response.getBody().getAccessToken();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String getProcessData(RestTemplate restTemplate, String accessToken, String instanceId) {
        HttpHeaders headers = new HttpHeaders();
        headers.setBearerAuth(accessToken);
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<Void> request = new HttpEntity<>(headers);

        try {
            ResponseEntity<String> response = restTemplate.getForEntity(PROCESS_DATA_URL, String.class, instanceId);
            if (response.getStatusCode().is2xxSuccessful()) {
                return response.getBody();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static boolean updateProcessData(RestTemplate restTemplate, String accessToken, String instanceId, String updatedData) {
        HttpHeaders headers = new HttpHeaders();
        headers.setBearerAuth(accessToken);
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<String> request = new HttpEntity<>(updatedData, headers);

        try {
            ResponseEntity<Void> response = restTemplate.put(PROCESS_DATA_URL, request, instanceId);
            return response.getStatusCode().is2xxSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    // 解析令牌响应的实体类
    private static class TokenResponse {
        private String access_token;

        public String getAccessToken() {
            return access_token;
        }

        public void setAccessToken(String access_token) {
            this.access_token = access_token;
        }
    }
}

关键注意事项

  • 替换代码中PM4_BASE_URL为你的docker容器实际对外暴露的地址和端口。
  • 确认CLIENT_ID:PM4默认客户端ID为pm4-client,可在后台"Auth Clients"模块查看详情。
  • 流程数据的具体API端点可通过PM4后台的Swagger文档(通常在/swagger-ui.html路径)查看完整接口列表。
  • 如果PM4启用了HTTPS,需将URL协议改为https,并处理证书相关配置。

内容的提问来源于stack exchange,提问作者Javaz81

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 04:55:18