【问题标题】:Are there Java classes for Camunda Rest responses?是否有用于 Camunda Rest 响应的 Java 类?
【发布时间】:2019-10-23 14:41:20
【问题描述】:

我目前正在尝试使用 camunda rest api 设置从 Spring 到本地 camunda 实例的 REST 调用。

我是这样设置的:

  1. 在我的localhost:8080 上启动了一个本地 camunda docker 容器,如下所示:https://hub.docker.com/r/camunda/camunda-bpm-platform (我已经用邮递员测试了电话,他们正在工作)

  2. 在我的pom.xml 中使用几个camunda 和rest 依赖项构建了一个maven 项目:

        <dependency>
            <groupId>org.camunda.bpm.springboot</groupId>
            <artifactId>camunda-bpm-spring-boot-starter</artifactId>
            <version>3.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.camunda.bpm</groupId>
            <artifactId>camunda-engine-rest-core</artifactId>
            <version>7.11.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectreactor</groupId>
            <artifactId>reactor-spring</artifactId>
            <version>1.0.1.RELEASE</version>
        </dependency>
  1. 编写了一个简单的服务来从 Spring 进行休息调用(取自 https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-webclient):
import org.camunda.bpm.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@Service
public class MyExampleService {

    private final WebClient webClient;

    public MyExampleService (WebClient.Builder webClientBuilder) {
        this.webClient = webClientBuilder.baseUrl("http://localhost:8080").build();
    }

    @Override
    public ProcessDefinitionEntity[] getCamundaProcesses() {

        ProcessDefinitionEntity[] myResponse = this.webClient.get().uri("/engine-rest/process-definition/")
                .retrieve()
                .onStatus(HttpStatus::is4xxClientError, response -> {
                    System.out.println("4xx eror");
                    return Mono.error(new RuntimeException("4xx"));
                })
                .onStatus(HttpStatus::is5xxServerError, response -> {
                    System.out.println("5xx eror");
                    return Mono.error(new RuntimeException("5xx"));
                })
                .bodyToMono(ProcessDefinitionEntity[].class)
                .block();

        return myResponse;
}

所以我基本上使用 Spring WebClient 对 localhost:8080/engine-rest/deployment/ 进行休息调用,这应该给我一个 JSON 格式的所有进程列表(根据 https://docs.camunda.org/manual/latest/reference/rest/deployment/get-query/)。

现在,当我将响应直接转换为 ProcessDefinitionEntity[] 时,它不会将 JSON 转换为它。我还尝试了来自 camunda Java API (https://docs.camunda.org/javadoc/camunda-bpm-platform/7.11/) 的其他类,例如 ProcessDefinitionDto

似乎没有一个课程适合我从 camunda 得到的回复。响应如下所示:

[
    {
        "id": "invoice:1:cdbc3f02-e6a1-11e9-8de8-0242ac110002",
        "key": "invoice",
        "category": "http://www.omg.org/spec/BPMN/20100524/MODEL",
        "description": null,
        "name": "Invoice Receipt",
        "version": 1,
        "resource": "invoice.v1.bpmn",
        "deploymentId": "cda115de-e6a1-11e9-8de8-0242ac110002",
        "diagram": null,
        "suspended": false,
        "tenantId": null,
        "versionTag": "V1.0",
        "historyTimeToLive": 30,
        "startableInTasklist": true
    },
    {
        "id": "invoice:2:ce03f66c-e6a1-11e9-8de8-0242ac110002",
        "key": "invoice",
        "category": "http://www.omg.org/spec/BPMN/20100524/MODEL",
        "description": null,
        "name": "Invoice Receipt",
        "version": 2,
        "resource": "invoice.v2.bpmn",
        "deploymentId": "cdfbb908-e6a1-11e9-8de8-0242ac110002",
        "diagram": null,
        "suspended": false,
        "tenantId": null,
        "versionTag": "V2.0",
        "historyTimeToLive": 45,
        "startableInTasklist": true
    }
]

(这只是 docker 容器中的两个标准进程)

camunda java api 中是否有正确匹配来自 camunda rest api 的响应的类?

PS: 我从 maven 依赖项(位于package org.camunda.bpm.engine.rest.dto.repository.ProcessDefinitionDto)中获得的 ProcessDefinitionDto 如下所示:

public class ProcessDefinitionDto {
    protected String id;
    protected String key;
    protected String category;
    protected String description;
    protected String name;
    protected int version;
    protected String resource;
    protected String deploymentId;
    protected String diagram;
    protected boolean suspended;
    protected String tenantId;
    protected String versionTag;
    protected Integer historyTimeToLive;
    protected boolean isStartableInTasklist;
...

我从邮递员致电http://localhost:8080/engine-rest/process-definition 得到的响应如下所示:

[
    {
        "id": "b506eb34-e6b1-11e9-8de8-0242ac110002",
        "key": "example_workflow",
        "category": "http://bpmn.io/schema/bpmn",
        "description": null,
        "name": "Just an Example",
        "version": 1,
        "resource": "example_workflow.bpmn",
        "deploymentId": "b503b6e2-e6b1-11e9-8de8-0242ac110002",
        "diagram": null,
        "suspended": false,
        "tenantId": null,
        "versionTag": null,
        "historyTimeToLive": null,
        "startableInTasklist": true
    },
    {
        "id": "invoice:1:cdbc3f02-e6a1-11e9-8de8-0242ac110002",
        "key": "invoice",
        "category": "http://www.omg.org/spec/BPMN/20100524/MODEL",
        "description": null,
        "name": "Invoice Receipt",
        "version": 1,
        "resource": "invoice.v1.bpmn",
        "deploymentId": "cda115de-e6a1-11e9-8de8-0242ac110002",
        "diagram": null,
        "suspended": false,
        "tenantId": null,
        "versionTag": "V1.0",
        "historyTimeToLive": 30,
        "startableInTasklist": true
    },
    {
        "id": "invoice:2:ce03f66c-e6a1-11e9-8de8-0242ac110002",
        "key": "invoice",
        "category": "http://www.omg.org/spec/BPMN/20100524/MODEL",
        "description": null,
        "name": "Invoice Receipt",
        "version": 2,
        "resource": "invoice.v2.bpmn",
        "deploymentId": "cdfbb908-e6a1-11e9-8de8-0242ac110002",
        "diagram": null,
        "suspended": false,
        "tenantId": null,
        "versionTag": "V2.0",
        "historyTimeToLive": 45,
        "startableInTasklist": true
    }
]

【问题讨论】:

    标签: json spring rest camunda spring-webclient


    【解决方案1】:

    中的类

    org.camunda.bpm.engine.rest.dto.runtime

    适合。 (https://github.com/camunda/camunda-bpm-platform/tree/master/engine-rest/engine-rest/src/main/java/org/camunda/bpm/engine/rest/dto)

    <dependency>
      <groupId>org.camunda.bpm</groupId>
      <artifactId>camunda-engine-rest-core</artifactId>
    </dependency>
    

    在您的情况下,它将是 ProcessDefinitionDto Spring REST 模板的使用示例:

    @Service
    @Slf4j
    public class RuntimeServiceImpl implements RuntimeService {
    
        private final RestTemplate rest;
        @Value("${camunda.server.rest.url}")
        private String restURL;
    
        public RuntimeServiceImpl(RestTemplateBuilder builder) {
            this.rest = builder.build();
        }
    
        public ProcessDefinitionDto[] getProcessDefinitions() {
    
            ResponseEntity<ProcessDefinitionDto[]> response = rest.getForEntity(restURL + "process-definition/",
                    ProcessDefinitionDto[].class);
            ProcessDefinitionDto[] processes = response.getBody();
    
            Arrays.stream(processes).forEach(pd -> log.info("Found process definition {} with id {} and key {}", pd.getName(), pd.getId(), pd.getKey()));
    
            return processes;
        }
    }
    

    完整的客户项目:https://github.com/rob2universe/camunda-rest-client

    external class client 包含更多可以重复使用的接口和 DTO,例如

    org.camunda.bpm.client.topic.impl.dto.FetchAndLockRequestDto; org.camunda.bpm.client.topic.impl.dto.TopicRequestDto;

    或者只是使用 / fork 完整的客户端项目。那里已经为您完成了很多工作。

    附:以下评论: 响应包含一个流程定义数组,其中包含以下字段:

    [
        {
            "id": "AccountOpening:1:a6f88770-0ae7-11ea-9ef3-00155d00d800",
            "key": "AccountOpening",
            "category": "http://bpmn.io/schema/bpmn",
            "description": null,
            "name": "Account Opening",
            "version": 1,
            "resource": "C:\\account-opening\\target\\classes\\bpmn\\AccountOpening.bpmn",
            "deploymentId": "a6cc4747-0ae7-11ea-9ef3-00155d00d800",
            "diagram": null,
            "suspended": false,
            "tenantId": null,
            "versionTag": null,
            "historyTimeToLive": null,
            "startableInTasklist": true
        }
    

    org.camunda.bpm.engine.rest.dto.repository中的目标数据结构包含相同的字段:

    public class ProcessDefinitionDto {
        protected String id;
        protected String key;
        protected String category;
        protected String description;
        protected String name;
        protected int version;
        protected String resource;
        protected String deploymentId;
        protected String diagram;
        protected boolean suspended;
        protected String tenantId;
        protected String versionTag;
        protected Integer historyTimeToLive;
        protected boolean isStartableInTasklist;
       ...}
    

    如果反序列化失败,请确保您使用的是正确的包(例如 org.camunda.bpm.cockpit.impl.plugin.base.dto)

    【讨论】:

    • 当我对http://localhost:8080/engine-rest/process-definition/ 进行GET 调用时,结果不适用于Java 类List&lt;ProcessInstanceWithVariablesDto&gt;
    • 我更新了我对您的具体案例的回答,并在 GitHub 上添加了一个示例客户端项目
    • 抱歉这么晚才回复。我正忙着做我的应用程序的其他部分。所以我从你的 RuntimeServiceImpl 类中复制并尝试了解决方案。但是这似乎不起作用:link 我得到 3 个结果(这是正确的,因为我的本地 camunda 实例中有 3 个进程)但是它们没有正确地转换到 ProcessDefinitionDto 数组中:(
    • 如果您使用正确的查询,那么反序列化应该可以工作。您可以分享您的代码的任何机会,例如通过github?
    • 我无法分享我的整个项目(因为版权问题),但我为我的问题创建了一个包含所有相关文件的文件夹:link 正如你所看到的,我的服务实际上只做这个简单的事情调用camunda的rest api。我还注释掉了实际有效的代码。在那里,我创建了一个自定义 ProcessDefinition 类并将 JSON 响应投射到其中,该 DID 工作(但恕我直言是一个 hacky 解决方案)
    【解决方案2】:

    您查看的是正确的包裹吗? Camunda 代码库中有多个 ProcessDefinitionDto 类。 您正在寻找的那个在 camunda-engine-rest-core.jar 的包 org.camunda.bpm.engine.rest.dto.repository

    该类看起来像这样,与您的输出完全匹配:

    package org.camunda.bpm.engine.rest.dto.repository;
    
    import org.camunda.bpm.engine.repository.ProcessDefinition;
    
    public class ProcessDefinitionDto {
    
      protected String id;
      protected String key;
      protected String category;
      protected String description;
      protected String name;
      protected int version;
      protected String resource;
      protected String deploymentId;
      protected String diagram;
      protected boolean suspended;
      protected String tenantId;
      protected String versionTag;
      protected Integer historyTimeToLive;
      protected boolean isStartableInTasklist;
    
      ...
    

    【讨论】:

    • 这个类并不完全符合我的 json 响应。响应有startableInTasklist,而这个类称它为isStartableInTasklist
    • 这只是一个命名约定。如果您检查其余接口(camunda-engine-rest-core 中的ProcessDefinitionRestServiceImpl.java)包的源代码,您会看到使用的是正确的类。 Jax-ws 将在休息响应中将此属性转换为 startableInTasklist。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 2020-12-01
    • 2023-04-06
    • 2022-01-10
    • 2021-02-18
    相关资源
    最近更新 更多