0

I am trying to create a custom REST API in Camunda. My Camunda application is standalone and I want to create a custom REST API to complete task and send response.

I created a separate REST Project to create an endpoint which an external application can call and complete a task and get response back.

https://github.com/harish2610/camunda-custom-rest/tree/master/camunda_rest_endpoint/src/main/java/com/camunda/custom/rest/endpoint

When I run this project, I get the following error .

Field engine in com.camunda.custom.rest.endpoint.service.TaskCompletionService required a bean of type 'org.camunda.bpm.engine.ProcessEngine' that could not be found.

I have another project where I have Camunda standalone application and I want my custom REST endpoint to act on the task of the workflow defined under this project:

https://github.com/harish2610/camunda-custom-rest/tree/master/my-project/src/main/java/com/example/workflow

My REST endpoint should look like this :

http://localhost:8088/engine-rest/api/completeTask

My questions are :

  1. Is it achievable to build a custom REST API endpoint in Camunda ?
  2. If so, How do I integrate my REST project and Camunda project together, so that they work together and give a response.

Please let me know what am I doing wrong in my approach ? Or is there any better approach to create a custom rest endpoint in Camunda ?

This is how I am trying to trigger the API from postman.

enter image description here

Thanks

Voyager3
  • 109
  • 1
  • 11

1 Answers1

1

A) The custom rest API needs to make use of the process engine. It can only do so if it is running in the same Spring context as the process engine. Currently you have two complete separate Spring Boot projects. You can fix this in different ways.

B) Camunda and Spring use different frameworks to expose REST services. Camunda uses a standard JAX-RS implementation, so it can also work without Spring. Spring uses its own implementation provided by Spring MVC (see Difference between JAX-RS and Spring Rest)
engine-rest is the context under which the process engine exposes its JAX-RS based REST api. You cannot mix Spring endpoints under the same context.

  • either use Spring, but expose the api under a separate context
  • or make your REST service use the JAX-RS (Jersey) implementation sued by Camunda.

In this example I am using a Maven multi-module project and extending the Camunda JAX-RS REST api with a custom service https://github.com/rob2universe/camunda-custom-rest-endpoint/tree/main

However, if you are writing a custom Facade, you may not plan to expose the Camunda REST API to clients at all. IN that case it would be better to keep your custom services under a separate context, so you can only expose that context and not allow access to the Camunda REST API from outside.

rob2universe
  • 7,059
  • 39
  • 54
  • Thanks @rob2universe for such a detailed response. I tried to emulate your example given in the github link and created my project on similar concept i.e. maven multi-module project. https://github.com/harish2610/customRestApi But I am not able to trigger the custom REST endpoint. Could you please take a look and let me know what am I missing in my project ? – Voyager3 May 10 '23 at 01:06
  • I don't get any error but the REST Controller doesn't get triggered. To test the API from Postman I am using the following endpoint : http://localhost:8080/engine-rest/api/completeTask with the following JSON as part of the POST request : { "taskId": "8729d55d-eeaa-11ed-ba58-3ce9f77d27d3", "variables": { "1": "A" } } And I am getting the following as response: { "type": "NotFoundException", "message": "HTTP 404 Not Found", "code": null } – Voyager3 May 10 '23 at 01:19
  • do you see the system out you placed in the resources config customizer? Does the log show it is loaded? – rob2universe May 10 '23 at 09:16
  • Thanks @rob2universe. We are able to make it work successfully. Thanks a lot – Voyager3 May 10 '23 at 19:59
  • Hi @rob2universe I tried the first approach as well suggested by you where we can add the custom rest project as a jar dependency to the engine project but it did not trigger the resources config customizer. Is there any step I need to do apart from adding maven dependency and the jar file as an external library ? I placed my project at the following location for reference. https://github.com/harish2610/camunda-custom-rest/tree/master/camunda-project Thanks – Voyager3 May 17 '23 at 18:52
  • @Voyager3 - make sure the Spring bean you are declaring via https://github.com/rob2universe/camunda-custom-rest-endpoint/blob/9a6c3aa22c9ef218cabdf871f71a4699d160d82b/custom-endpoint/src/main/java/org/camunda/example/api/config/MyResourceConfigCustomizer.java#L8 is indeed created. If the package name differs and your class is not part of Spring's component scan (https://www.baeldung.com/spring-component-scanning), then the bean will not be created. – rob2universe May 18 '23 at 00:49