camunda如何调用HTTP REST(Service Task)服务节点
Camunda中的Service Task(服务任务)用于调用服务。在Camunda中,可以通过调用本地Java代码、外部工作项、web服务形式实现的逻辑来完成的。
本文重点描述如何使用web服务实现Camunda服务调用,即如何使用camunda Connector连接器调用HTTP服务,本地Java代码和外部工作项后续文章中再详细介绍。
Camunda Connect提供HTTP和SOAP HTTP连接器。本示例演示了使用http连接器从Camunda BPM中的服务任务调用REST服务。
一、添加项目依赖
给项目中添加camunda connect 和 camunda spin 包依赖,重新启动camunda Platform。
<dependency>
<groupId>org.camunda.bpm</groupId>
<artifactId>camunda-engine-plugin-connect</artifactId>
<version>7.15.0</version>
</dependency>
<dependency>
<groupId>org.camunda.bpm</groupId>
<artifactId>camunda-engine-plugin-spin</artifactId>
<version>7.15.0</version>
</dependency>
<dependency>
<groupId>org.camunda.spin</groupId>
<artifactId>camunda-spin-dataformat-json-jackson</artifactId>
<version>1.10.1</version>
</dependency>
<dependency>
<groupId>org.camunda.spin</groupId>
<artifactId>camunda-spin-core</artifactId>
<version>1.10.1</version>
</dependency>
<dependency>
<groupId>org.camunda.spin</groupId>
<artifactId>camunda-spin-dataformat-all</artifactId>
<version>1.10.1</version>
</dependency>
二、设计流程图

编辑
以下时服务节点的关键配置项:
HTTP连接器可用于创建新请求、设置HTTP方法、URL、内容类型和有效负载。
The HTTP connector can be used to create a new request, set a HTTP method, URL, content type and payload.
|
Parameter |
Description |
|
method |
Sets the HTTP method of the request |
|
url |
Sets the URL of the request |
|
headers |
Contains a map of the configured HTTP headers of the request |
|
payload |
Sets the payload of the request |

编辑

编辑

编辑

编辑
返回值包含状态码、响应头和响应体。
A response contains the status code, response headers and body.
|
Parameter |
Description |
|
statusCode |
Contains the status code of the response |
|
headers |
Contains a map with the HTTP headers of the response |
|
response |
Contains the response body |

编辑
以下是完整的BPMN模型文件:
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_1qvw7a2" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.8.1" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.15.0">
<bpmn:process id="Process_0a6gw7u" name="贷款申请流程HTTP" isExecutable="true">
<bpmn:startEvent id="StartEvent_1">
<bpmn:outgoing>Flow_0kzdck2</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:sequenceFlow id="Flow_0kzdck2" sourceRef="StartEvent_1" targetRef="Activity_0bvh293" />
<bpmn:sequenceFlow id="Flow_07gaddc" sourceRef="Activity_0bvh293" targetRef="Activity_0kimj7k" />
<bpmn:serviceTask id="Activity_0bvh293" name="贷款额度计算">
<bpmn:extensionElements>
<camunda:connector>
<camunda:inputOutput>
<camunda:inputParameter name="headers">
<camunda:map>
<camunda:entry key="Content-Type">text/plain</camunda:entry>
<camunda:entry key="Accept">application/json</camunda:entry>
</camunda:map>
</camunda:inputParameter>
<camunda:inputParameter name="method">POST</camunda:inputParameter>
<camunda:inputParameter name="url">http://127.0.0.1:8888/userLoan/computeLoan?yearWages=${yearWages}&houseAssets=${houseAssets}</camunda:inputParameter>
<camunda:outputParameter name="creditRating">
<camunda:script scriptFormat="JavaScript">var response = connector.getVariable("response");
var user = S(response);
var creditRating = user.prop("creditRating").stringValue();
creditRating;</camunda:script>
</camunda:outputParameter>
<camunda:outputParameter name="loanLimit">
<camunda:script scriptFormat="JavaScript">var response = connector.getVariable("response");
var user = S(response);
var loanLimit = user.prop("loanLimit").numberValue();
loanLimit;</camunda:script>
</camunda:outputParameter>
<camunda:outputParameter name="loanUser">${response}</camunda:outputParameter>
</camunda:inputOutput>
<camunda:connectorId>http-connector</camunda:connectorId>
</camunda:connector>
</bpmn:extensionElements>
<bpmn:incoming>Flow_0kzdck2</bpmn:incoming>
<bpmn:outgoing>Flow_07gaddc</bpmn:outgoing>
</bpmn:serviceTask>
<bpmn:userTask id="Activity_0kimj7k" name="确认贷款额度" camunda:assignee="demo">
<bpmn:incoming>Flow_07gaddc</bpmn:incoming>
<bpmn:outgoing>Flow_09mofy3</bpmn:outgoing>
</bpmn:userTask>
<bpmn:sequenceFlow id="Flow_09mofy3" sourceRef="Activity_0kimj7k" targetRef="Event_0myx83u" />
<bpmn:endEvent id="Event_0myx83u">
<bpmn:incoming>Flow_09mofy3</bpmn:incoming>
</bpmn:endEvent>
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_0a6gw7u">
<bpmndi:BPMNEdge id="Flow_09mofy3_di" bpmnElement="Flow_09mofy3">
<di:waypoint x="570" y="117" />
<di:waypoint x="662" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_07gaddc_di" bpmnElement="Flow_07gaddc">
<di:waypoint x="370" y="117" />
<di:waypoint x="470" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_0kzdck2_di" bpmnElement="Flow_0kzdck2">
<di:waypoint x="215" y="117" />
<di:waypoint x="270" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
<dc:Bounds x="179" y="99" width="36" height="36" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_0rfgq1k_di" bpmnElement="Activity_0bvh293">
<dc:Bounds x="270" y="77" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_16ovae8_di" bpmnElement="Activity_0kimj7k">
<dc:Bounds x="470" y="77" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_0myx83u_di" bpmnElement="Event_0myx83u">
<dc:Bounds x="662" y="99" width="36" height="36" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>

三、开发HTTP服务
1. 新建springboot工程,开发User类
import java.io.Serializable;
public class User implements Serializable {
private String userName; //姓名
private int userAge; //年龄
private double yearWages; //年薪
private double houseAssets; //房产
private String creditRating; //信用等级
private double loanLimit; //贷款额度
private String isTransferAccount; //是否已放贷
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getUserAge() {
return userAge;
}
public void setUserAge(int userAge) {
this.userAge = userAge;
}
public double getYearWages() {
return yearWages;
}
public void setYearWages(double yearWages) {
this.yearWages = yearWages;
}
public double getHouseAssets() {
return houseAssets;
}
public void setHouseAssets(double houseAssets) {
this.houseAssets = houseAssets;
}
public String getCreditRating() {
return creditRating;
}
public void setCreditRating(String creditRating) {
this.creditRating = creditRating;
}
public double getLoanLimit() {
return loanLimit;
}
public void setLoanLimit(double loanLimit) {
this.loanLimit = loanLimit;
}
public String getIsTransferAccount() {
return isTransferAccount;
}
public void setIsTransferAccount(String isTransferAccount) {
this.isTransferAccount = isTransferAccount;
}
}

2. 开发REST服务类
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/userLoan")
public class UserLoanController {
@PostMapping("/computeLoan")
public User computeLoan(double yearWages,double houseAssets){
User user=new User();
user.setYearWages(yearWages);
user.setHouseAssets(houseAssets);
double sum = yearWages + houseAssets;
if(sum<=0){
user.setCreditRating("C");
user.setLoanLimit(0);
}else if(sum>0 && sum <=100){
user.setCreditRating("B");
user.setLoanLimit(sum*0.8);
}else if(sum>100){
user.setCreditRating("A");
user.setLoanLimit(sum*1.2);
}
return user;
}
}

四、发起流程测试
登录:http://localhost:8080/camunda/app/admin/default/#/login
1、发起流程,输入流程变量,后面的服务节点需要这两个流程变量

编辑
2、提交流程后,查看流程图,HTTP服务节点已经成功执行了

编辑
3、查看表单中的流程变量,HTTP服务节点计算后的返回值已经成功写入了。

编辑
更多参考:
Camunda Connector Reference | docs.camunda.org
camunda-bpm-examples/servicetask at 7.15 · camunda/camunda-bpm-examples · GitHub
camunda如何调用HTTP REST(Service Task)服务节点的更多相关文章
- 翻译-使用Spring调用SOAP Web Service
原文链接: http://spring.io/guides/gs/consuming-web-service/ 调用SOAP web service 本指南将指导你使用Spring调用一个基于SOAP ...
- C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程
前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...
- WebService学习总结(四)——调用第三方提供的webService服务
http://www.cnblogs.com/xdp-gacl/p/4260627.html 互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他 ...
- java程序调用xfire发布的webService服务(二)
在上一篇的调用xfire发布的webService服务中,我只是从服务端返回了一个字符串给客户端,却没有测试从客户端传递数据给服务端.而实际应用中一般是不太可能只出现这样的应用场景的,因此我便更进一步 ...
- WebService学习--(四)调用第三方提供的webService服务
互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据和查询国内手机号码归属地为 ...
- C#作为客户端调用gsoap生成的C++服务端
近日在学习C++,偶然遇到网友想用C#调用gsoap生成的C++服务的问题,遂决定研究一下,网上搜索了很久,大多数是C++调用C#的应用.... 经过本人的不断努力,终于找到一种解决问题的方法,总结如 ...
- WebService学习总结——调用第三方提供的webService服务
互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据. 气象中心的管理系统将收 ...
- 延迟调用或多次调用第三方的Web API服务
当我们调用第三方的Web API服务的时候,不一定每次都是成功的.这时候,我们可能会再多尝试几次,也有可能延迟一段时间再去尝试调用服务. Task的静态方法Delay允许我们延迟执行某个Task,此方 ...
- 在Android中调用KSOAP2库访问webservice服务出现的服务端传入参数为null的问题解决
ksoap2-android-3.0.0-jar 第三方库来调用.net 写的Web Service 如果没有参数,那么调用一切顺利,但是如果服务是带参数的,那么服务端接收的参数都是nul. ...
随机推荐
- javaScript设计模式:发布订阅模式
发布订阅模式的思想是在观察者模式的基础上演变而来,在观察者模式中客户端监听到对象某个行为就触发对应任务程序.而在发布订阅模式中依然基于这个核心思想,所以有时候也会将两者认为是同一种设计模式.它们的不同 ...
- Linux安装Redis步骤和make遇到的坑
Linux安装Redis服务步骤 1.获取redis资源 cd /usr/local wget https://mirrors.huaweicloud.com/redis/redis-6 ...
- pycharm——import已存在的库居然失败!
问题 明明在cmd中可以import的库,放到pycharm中却找不到. 问题根源 找了一圈,最后得到这个结论. 因为pycharm默认就是这样的... 解决 打开设置,找到解释器 点击右边齿轮图标, ...
- 二. 手写SpringMVC框架
1.1 新建DispatcherServlet 1.2 在src目录下,新建applicationContext.xml <?xml version="1.0" encodi ...
- Linux磁盘分区fdisk命令操作(简洁版)
实例(环境为: CentOS Linux release 7.2.1511 (Core), 3.10.0-327.el7.x86_64) 选择要具体操作的第二块磁盘(linux下一切是文件形式对应): ...
- RMI反序列化学习
RMI学习 1.RMI简介 RMI(Remote Method Invocation),远程方法调用方法,其实就是本地java虚拟机要调用其他java虚拟机的方法,两个虚拟机可以是运行在相同计算机上的 ...
- python爬虫---字体反爬
目标地址:http://glidedsky.com/level/web/crawler-font-puzzle-1 打开google调试工具检查发现网页上和源码之中的数字不一样, 已经确认该题目为 字 ...
- Codeforces Round #741 (Div. 2), problem: (D1) Two Hundred Twenty One (easy version), 1700
Problem - D1 - Codeforces 题意: 给n个符号(+或-), +代表+1, -代表-1, 求最少删去几个点, 使得 题解(仅此个人理解): 1. 这题打眼一看, 肯定和奇 ...
- 2021.12.10 P2516 [HAOI2010]最长公共子序列(动态规划+滚动数组)
2021.12.10 P2516 [HAOI2010]最长公共子序列(动态规划+滚动数组) https://www.luogu.com.cn/problem/P2516 题意: 给定字符串 \(S\) ...
- Java web与web gis学习笔记(二)——百度地图API调用
系列链接: Java web与web gis学习笔记(一)--Tomcat环境搭建 Java web与web gis学习笔记(二)--百度地图API调用 JavaWeb和WebGIS学习笔记(三)-- ...