Camunda如何呼叫http服務

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。

org。camunda。bpm camunda-engine-plugin-connect 7。15。0 org。camunda。bpm camunda-engine-plugin-spin 7。15。0 org。camunda。spin camunda-spin-dataformat-json-jackson 1。10。1 org。camunda。spin camunda-spin-core 1。10。1 org。camunda。spin camunda-spin-dataformat-all 1。10。1

二、設計流程圖

Camunda如何呼叫http服務

以下時服務節點的關鍵配置項:

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

Camunda如何呼叫http服務

Camunda如何呼叫http服務

Camunda如何呼叫http服務

Camunda如何呼叫http服務

返回值包含狀態碼、響應頭和響應體。

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

Camunda如何呼叫http服務

以下是完整的BPMN模型檔案:

<?xml version=“1。0” encoding=“UTF-8”?> Flow_0kzdck2 text/plain application/json POST http://127。0。0。1:8888/userLoan/computeLoan?yearWages=${yearWages}&houseAssets=${houseAssets} var response = connector。getVariable(“response”);var user = S(response);var creditRating = user。prop(“creditRating”)。stringValue();creditRating; var response = connector。getVariable(“response”);var user = S(response);var loanLimit = user。prop(“loanLimit”)。numberValue();loanLimit; ${response} http-connector Flow_0kzdck2 Flow_07gaddc Flow_07gaddc Flow_09mofy3 Flow_09mofy3

三、開發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、發起流程,輸入流程變數,後面的服務節點需要這兩個流程變數

Camunda如何呼叫http服務

2、提交流程後,檢視流程圖,HTTP服務節點已經成功執行了

Camunda如何呼叫http服務

3、查看錶單中的流程變數,HTTP服務節點計算後的返回值已經成功寫入了。

Camunda如何呼叫http服務