我已经实现了通过camunda http连接器从camunda bpmn调用http REST端点的实现。
在这种情况下,首先,将调用BpmnInvoker
REST端点,并提供bpmnProcessId
作为路径参数,并将Authorization
键作为标头参数与requestBody一起使用
然后在BpmnInvoker
中,将通过requestBody值调用相关的bpmn图。在这些bpmn任务中,将有一些服务任务将调用REST端点。
要从BPMN图中调用REST端点,我使用camunda http连接器。在这种情况下,我需要将Authorization密钥作为http头传递给REST端点。
为此,我将Authorization键添加到变量映射中,并将其作为bpmn图中的变量,然后添加到镜像和bpmn图中提到的标头Map中。
但是问题在于,该授权的长度通常超过4000个字符。因此,不能通过camunda引擎将其作为String传递。 我尝试将其转换为另一个对象(例如:byte [] / StringBuffer等),假设它将存储为blob,但是在这种情况下,当它打算分配给http标头时,它会给出Class Cast Exception。
BPMNInvoke.java
@Path("/base")
public class BpmnInvoker {
ProcessEngine defaultProcessEngine = BpmPlatform.getProcessEngineService().getDefaultProcessEngine();
RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
@POST
@Path("/path/{bpmnProcessId}")
public Response start(@PathParam("bpmnProcessId") String bpmnProcessId, String requestBody, @HeaderParam("Authorization") String authorization) {
Map<String, Object> variableMap = new HashMap<String, Object>();
variableMap = IntegrationUtility.convertJSONStringToMap(requestBody);
// Set the Authorization token to the variable map.
// Normally this is more than 4000 characters long dynamic key.
// It must be set as a http header value when invoking REST endpoint via http connector.
variableMap.put("Authorization", authorization);
// Invoke the bpmn diagram by calling the startProcessInstanceByKey on runtimeService.
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(bpmnProcessId, variableMap);
// rest of the work after completing the process engine
}
}
----
----
<bpmn:serviceTask id="Task_1ujngj7" name="Dummy Service 2">
<bpmn:extensionElements>
<camunda:connector>
<camunda:inputOutput>
<camunda:inputParameter name="url">http://localhost:8080/x-services/path/to/rest/service</camunda:inputParameter>
<camunda:inputParameter name="method">POST</camunda:inputParameter>
<camunda:inputParameter name="headers">
<camunda:map>
<camunda:entry key="Authorization">${Authorization}</camunda:entry>
<camunda:entry key="Content-Type">application/json</camunda:entry>
</camunda:map>
</camunda:inputParameter>
<camunda:inputParameter name="payload">${payload}</camunda:inputParameter>
<camunda:outputParameter name="payload">${response}</camunda:outputParameter>
</camunda:inputOutput>
<camunda:connectorId>http-connector</camunda:connectorId>
</camunda:connector>
</bpmn:extensionElements>
<bpmn:incoming>SequenceFlow_0w4wfbo</bpmn:incoming>
<bpmn:outgoing>SequenceFlow_153ti9s</bpmn:outgoing>
</bpmn:serviceTask>
<bpmn:sequenceFlow id="SequenceFlow_1kvov2r" sourceRef="Task_0i9s02e" targetRef="EndEvent_0o062nq" />
<bpmn:serviceTask id="Task_0i9s02e" name="DummyServiceClear" camunda:delegateExpression="${dummyServiceClear}">
<bpmn:incoming>SequenceFlow_153ti9s</bpmn:incoming>
<bpmn:outgoing>SequenceFlow_1kvov2r</bpmn:outgoing>
</bpmn:serviceTask>
----
----
答案 0 :(得分:1)
尝试一下:
import org.camunda.bpm.engine.variable.Variables;
import org.camunda.bpm.engine.variable.Variables.SerializationDataFormats;
variableMap.put("Authorization", Variables
.objectValue(authorization)
// tells the engine to use java serialization for persisting the value
.serializationDataFormat(SerializationDataFormats.JAVA)
.create());