【代码评审修复】 Http 触发器返回值处理修改

This commit is contained in:
jason 2025-01-26 15:36:14 +08:00
parent f8d6f1e2c4
commit 44486afd95
2 changed files with 39 additions and 35 deletions

View File

@ -199,4 +199,11 @@ public class JsonUtils {
return JSONUtil.isTypeJSON(text); return JSONUtil.isTypeJSON(text);
} }
/**
* 判断字符串是否为 JSON 类型的字符串
* @param str 字符串
*/
public static boolean isJsonObject(String str) {
return JSONUtil.isTypeJSONObject(str);
}
} }

View File

@ -2,14 +2,14 @@ package cn.iocoder.yudao.module.bpm.service.task.trigger;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.core.KeyValue; import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils; import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.simple.BpmSimpleModelNodeVO.TriggerSetting.HttpRequestTriggerSetting; import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.simple.BpmSimpleModelNodeVO.TriggerSetting.HttpRequestTriggerSetting;
import cn.iocoder.yudao.module.bpm.enums.definition.BpmTriggerTypeEnum; import cn.iocoder.yudao.module.bpm.enums.definition.BpmTriggerTypeEnum;
import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.SimpleModelUtils; import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.SimpleModelUtils;
import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService; import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService;
import com.fasterxml.jackson.core.type.TypeReference;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.runtime.ProcessInstance; import org.flowable.engine.runtime.ProcessInstance;
@ -37,8 +37,6 @@ import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.HEADER_
@Slf4j @Slf4j
public class BpmHttpRequestTrigger implements BpmTrigger { public class BpmHttpRequestTrigger implements BpmTrigger {
private static final String PARSE_RESPONSE_FIELD = "data";
@Resource @Resource
private BpmProcessInstanceService processInstanceService; private BpmProcessInstanceService processInstanceService;
@ -72,54 +70,53 @@ public class BpmHttpRequestTrigger implements BpmTrigger {
// TODO @芋艿要不要抽象一个 Http 请求的工具类方便复用呢 // TODO @芋艿要不要抽象一个 Http 请求的工具类方便复用呢
// 3. 发起请求 // 3. 发起请求
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(body, headers); HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(body, headers);
ResponseEntity<String> responseEntity;
try { try {
ResponseEntity<String> responseEntity = restTemplate.exchange(setting.getUrl(), HttpMethod.POST, responseEntity = restTemplate.exchange(setting.getUrl(), HttpMethod.POST,
requestEntity, String.class); requestEntity, String.class);
log.info("[execute][HTTP 触发器,请求头:{},请求体:{},响应结果:{}]", headers, body, responseEntity); log.info("[execute][HTTP 触发器,请求头:{},请求体:{},响应结果:{}]", headers, body, responseEntity);
// TODO @jason建议把请求和失败放在两个 try catch 里处理
// 4. 处理请求返回
// TODO @jason返回结果要不统一用 CommonResult符合这个规范这样就可以验证 code 0 必须符合这个规范~~
if (CollUtil.isNotEmpty(setting.getResponse()) && responseEntity.getStatusCode().is2xxSuccessful()
&& StrUtil.isNotEmpty(responseEntity.getBody())) {
// 4.1 获取需要更新的流程变量
Map<String, Object> updateVariables = getNeedUpdatedVariablesFromResponse(responseEntity.getBody(), setting.getResponse());
// 4.2 更新流程变量
if (CollUtil.isNotEmpty(updateVariables)) {
processInstanceService.updateProcessInstanceVariables(processInstanceId, updateVariables);
}
}
} catch (RestClientException e) { } catch (RestClientException e) {
log.error("[execute][HTTP 触发器,请求头:{},请求体:{},请求出错:{}]", headers, body, e.getMessage()); log.error("[execute][HTTP 触发器,请求头:{},请求体:{},请求出错:{}]", headers, body, e.getMessage());
return;
}
// 4.1 判断是否需要解析返回值
if (StrUtil.isEmpty(responseEntity.getBody()) || !responseEntity.getStatusCode().is2xxSuccessful()
|| CollUtil.isEmpty(setting.getResponse())) {
return;
}
// 4.2 解析返回值, 返回值必须符合 CommonResult 规范
CommonResult<Map<String, Object>> respResult = JsonUtils.parseObjectQuietly(responseEntity.getBody(),
new TypeReference<>() {});
if (respResult == null || !respResult.isSuccess()){
return;
}
// 4.3 获取需要更新的流程变量
Map<String, Object> updateVariables = getNeedUpdatedVariablesFromResponse(respResult.getData(), setting.getResponse());
// 4.4 更新流程变量
if (CollUtil.isNotEmpty(updateVariables)) {
processInstanceService.updateProcessInstanceVariables(processInstanceId, updateVariables);
} }
} }
/** /**
* 从请求返回值获取需要更新的流程变量 * 从请求返回值获取需要更新的流程变量
* *
* 优先从 data 字段获取如果 data 字段不存在从根节点获取 * @param result 请求返回结果
*
* @param responseBody 请求返回报文体
* @param responseSettings 返回设置 * @param responseSettings 返回设置
* @return 需要更新的流程变量 * @return 需要更新的流程变量
*/ */
private Map<String, Object> getNeedUpdatedVariablesFromResponse(String responseBody, private Map<String, Object> getNeedUpdatedVariablesFromResponse(Map<String,Object> result,
List<KeyValue<String, String>> responseSettings) { List<KeyValue<String, String>> responseSettings) {
Map<String, Object> updateVariables = new HashMap<>(); Map<String, Object> updateVariables = new HashMap<>();
// TODO @jason这里 if return 更简洁一点 if (CollUtil.isEmpty(result)) {
// TODO @jasonJSONUtil => JsonUtils尽量包一层 return updateVariables;
if (JSONUtil.isTypeJSONObject(responseBody)) {
JSONObject dataObj = null;
if (JSONUtil.parseObj(responseBody).getObj(PARSE_RESPONSE_FIELD) instanceof JSONObject) {
dataObj = (JSONObject) JSONUtil.parseObj(responseBody).getObj(PARSE_RESPONSE_FIELD);
}
JSONObject updateObj = dataObj == null ? JSONUtil.parseObj(responseBody) : dataObj;
responseSettings.forEach(responseSetting -> {
if (StrUtil.isNotEmpty(responseSetting.getKey()) && updateObj.containsKey(responseSetting.getValue())) {
updateVariables.put(responseSetting.getKey(), updateObj.get(responseSetting.getValue()));
}
});
} }
responseSettings.forEach(responseSetting -> {
if (StrUtil.isNotEmpty(responseSetting.getKey()) && result.containsKey(responseSetting.getValue())) {
updateVariables.put(responseSetting.getKey(), result.get(responseSetting.getValue()));
}
});
return updateVariables; return updateVariables;
} }