diff --git a/src/main/java/weaver/xiao/commons/config/service/DealWithMapping.java b/src/main/java/weaver/xiao/commons/config/service/DealWithMapping.java index 7b1f579..a3a1254 100644 --- a/src/main/java/weaver/xiao/commons/config/service/DealWithMapping.java +++ b/src/main/java/weaver/xiao/commons/config/service/DealWithMapping.java @@ -1,6 +1,12 @@ package weaver.xiao.commons.config.service; +import aiyh.utils.excention.CustomerException; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.type.TypeFactory; import com.google.common.base.Strings; import lombok.AllArgsConstructor; import lombok.Data; @@ -57,7 +63,7 @@ public class DealWithMapping extends ToolUtil { private List fileNames = new ArrayList<>(); private List fileInputStreams = new ArrayList<>(); private List multipartFileList = new ArrayList<>(); - + private static final ObjectMapper objectMapper = new ObjectMapper(); private RecordSet tempRs; private DealWithMapper mapper = null; @@ -1451,7 +1457,10 @@ public class DealWithMapping extends ToolUtil { List detailResponseList = new ArrayList<>(); for (ResponseMapping responseMapping : responseMappingList) { if (responseMapping.getMainOrDetail() == 0) { - String resVal = Util.null2String(requestRes.get(responseMapping.getResponseFieldName())); + // 解析层级数据 + String fieldName = responseMapping.getResponseFieldName(); + Object result = parseCommon(requestRes, fieldName, Object.class); + String resVal = Util.null2String(result); mainUpdateMap.put(responseMapping.getWorkflowFieldName(), resVal); } else { detailResponseList.add(responseMapping); @@ -1475,6 +1484,100 @@ public class DealWithMapping extends ToolUtil { logger.info("回写信息tableUpdateMap==> " + tableUpdateMap); return tableUpdateMap; } + + /** + *

解析普通值

+ * @author xuanran.wang + * @dateTime 2023/7/7 17:18 + * @param map 响应map + * @param dataKey 值路径 + * @param clazz 值类型 + * @return 普通值 + **/ + public T parseCommon(Map map, String dataKey, Class clazz) { + if (MapUtils.isEmpty(map)) { + throw new CustomerException("map can not be null!"); + } + Object object = getDataObject(map, dataKey); + ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.convertValue(object, clazz); + } + + /** + *

解析对象

+ * @author xuanran.wang + * @dateTime 2023/7/7 17:18 + * @param map 响应map + * @param dataKey 值路径 + * @param clazz 值类型 + * @return 对象 + **/ + public T parseObj(Map map, String dataKey, Class clazz) { + if (MapUtils.isEmpty(map)) { + throw new CustomerException("map can not be null!"); + } + Object dataObject = getDataObject(map, dataKey); + if (Objects.isNull(dataObject)) { + throw new CustomerException("parse object error!"); + } + if (dataObject instanceof Map) { + return (T) JSONObject.parseObject(JSONObject.toJSONString(dataObject), clazz); + } + return null; + } + + /** + *

解析集合

+ * @author xuanran.wang + * @dateTime 2023/7/7 17:18 + * @param map 响应map + * @param dataKey 值路径 + * @param clazz 值类型 + * @return 集合 + **/ + public List parseList(Map map, String dataKey, Class clazz) { + if (MapUtils.isEmpty(map)) { + throw new CustomerException("map can not be null!"); + } + Object dataObject = getDataObject(map, dataKey); + if (Objects.isNull(dataObject)) { + throw new CustomerException("parse object error!"); + } + if (dataObject instanceof List) { + JavaType listType = TypeFactory.defaultInstance().constructParametricType(List.class, clazz); + try { + String jsonString = objectMapper.writeValueAsString(dataObject); + return objectMapper.readValue(jsonString, listType); + } catch (JsonProcessingException e) { + throw new CustomerException("JSON processing error!", e); + } + } + return null; + } + + /** + *

按照值路径解析对象

+ * @author xuanran.wang + * @dateTime 2023/7/7 17:19 + * @param map 响应map + * @param dataKey 值路径 + * @return 解析对象 + **/ + private Object getDataObject(Map map, String dataKey) { + if(StringUtils.isBlank(dataKey) || dataKey.split("\\.").length == 0){ + return map; + } + Object dataObject = map; + String[] keys = dataKey.split("\\."); + for (String key : keys) { + if (dataObject instanceof Map) { + dataObject = ((Map) dataObject).get(key); + } else { + return dataObject; + } + } + return dataObject; + } /** * 自定义时间格式化 diff --git a/src/test/java/xuanran/wang/NormalTest.java b/src/test/java/xuanran/wang/NormalTest.java index 17b6aac..13ed4cb 100644 --- a/src/test/java/xuanran/wang/NormalTest.java +++ b/src/test/java/xuanran/wang/NormalTest.java @@ -3,6 +3,7 @@ package xuanran.wang; import aiyh.utils.GenerateFileUtil; import basetest.BaseTest; import org.junit.Test; +import weaver.xuanran.wang.ajx.work_flow_split.action.ChildWorkFlowSplitAction; import weaver.xuanran.wang.eighty_five_degreec.sap.action.WorkflowToSap; import weaver.xuanran.wang.immc.WorkFlowToVmsAndMQ; @@ -17,6 +18,6 @@ public class NormalTest extends BaseTest { @Test public void testWord(){ - GenerateFileUtil.createActionDocument(WorkFlowToVmsAndMQ.class); + GenerateFileUtil.createActionDocument(ChildWorkFlowSplitAction.class); } } diff --git a/src/test/java/xuanran/wang/ajx/TestA.java b/src/test/java/xuanran/wang/ajx/TestA.java index ddb5d30..c60c564 100644 --- a/src/test/java/xuanran/wang/ajx/TestA.java +++ b/src/test/java/xuanran/wang/ajx/TestA.java @@ -4,17 +4,19 @@ import aiyh.utils.Util; import aiyh.utils.excention.CustomerException; import basetest.BaseTest; import com.alibaba.fastjson.JSONObject; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.type.TypeFactory; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; import org.junit.Test; import weaver.bokang.xiao.common.mapper.WorkflowMapper; import weaver.xuanran.wang.ajx.work_flow_split.mapper.ChildWorkFlowSplitMapper; import weaver.xuanran.wang.common.mapper.CommonMapper; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; /** @@ -27,6 +29,7 @@ public class TestA extends BaseTest { private final CommonMapper workflowMapper = Util.getMapper(CommonMapper.class); private final ChildWorkFlowSplitMapper mapper = Util.getMapper(ChildWorkFlowSplitMapper.class); + private static final ObjectMapper objectMapper = new ObjectMapper(); private String insertAccountNumberField = ""; @@ -82,4 +85,122 @@ public class TestA extends BaseTest { throw new CustomerException("批量插入明细数据失败!"); } } + + @Test + public void testB(){ + String json = "{\n" + + " \"code\": 200,\n" + + " \"msg\": \"接口返回成功\",\n" + + " \"status\": true,\n" + + " \"data\": {\n" + + " \"singleTenant\": \"false\",\n" + + " \"urlTitle\": 100,\n" + + " \"changeTenant\": \"true\",\n" + + " \"realUri\": \"http://www.e-cology.com.cn/papi/app/baseserver/info/getUrlInfo?service=https%3A%2F%2Fwww.e-cology.com.cn%2Fblog%2Ftheirblogs%3FcusMenuId%3D5115968866800923639%26urlPageTitle%3D5LuW5Lq65pel5oql\",\n" + + " \"onlineDomianUrl\": \"https://online.e-cology.com.cn\",\n" + + " \"weappUrl\": \"https://www.e-cology.com.cn\",\n" + + " \"passportUrl\": \"https://www.e-cology.com.cn\",\n" + + " \"deployEnv\": \"private\",\n" + + " \"createTenant\": \"false\"\n" + + " },\n" + + " \"fail\": false\n" + + "}"; + Map map = JSONObject.parseObject(json, Map.class); + Object o = parseCommon(map, "data.urlTitle", Object.class); + System.out.println(Util.null2String(o)); + } + + /** + *

解析普通值

+ * @author xuanran.wang + * @dateTime 2023/7/7 17:18 + * @param map 响应map + * @param dataKey 值路径 + * @param clazz 值类型 + * @return 普通值 + **/ + public T parseCommon(Map map, String dataKey, Class clazz) { + if (MapUtils.isEmpty(map)) { + throw new CustomerException("map can not be null!"); + } + Object object = getDataObject(map, dataKey); + ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.convertValue(object, clazz); + } + + /** + *

解析对象

+ * @author xuanran.wang + * @dateTime 2023/7/7 17:18 + * @param map 响应map + * @param dataKey 值路径 + * @param clazz 值类型 + * @return 对象 + **/ + public T parseObj(Map map, String dataKey, Class clazz) { + if (MapUtils.isEmpty(map)) { + throw new CustomerException("map can not be null!"); + } + Object dataObject = getDataObject(map, dataKey); + if (Objects.isNull(dataObject)) { + throw new CustomerException("parse object error!"); + } + if (dataObject instanceof Map) { + return (T) JSONObject.parseObject(JSONObject.toJSONString(dataObject), clazz); + } + return null; + } + + /** + *

解析集合

+ * @author xuanran.wang + * @dateTime 2023/7/7 17:18 + * @param map 响应map + * @param dataKey 值路径 + * @param clazz 值类型 + * @return 集合 + **/ + public List parseList(Map map, String dataKey, Class clazz) { + if (MapUtils.isEmpty(map)) { + throw new CustomerException("map can not be null!"); + } + Object dataObject = getDataObject(map, dataKey); + if (Objects.isNull(dataObject)) { + throw new CustomerException("parse object error!"); + } + if (dataObject instanceof List) { + JavaType listType = TypeFactory.defaultInstance().constructParametricType(List.class, clazz); + try { + String jsonString = objectMapper.writeValueAsString(dataObject); + return objectMapper.readValue(jsonString, listType); + } catch (JsonProcessingException e) { + throw new CustomerException("JSON processing error!", e); + } + } + return null; + } + + /** + *

按照值路径解析对象

+ * @author xuanran.wang + * @dateTime 2023/7/7 17:19 + * @param map 响应map + * @param dataKey 值路径 + * @return 解析对象 + **/ + private Object getDataObject(Map map, String dataKey) { + if(StringUtils.isBlank(dataKey) || dataKey.split("\\.").length == 0){ + return map; + } + Object dataObject = map; + String[] keys = dataKey.split("\\."); + for (String key : keys) { + if (dataObject instanceof Map) { + dataObject = ((Map) dataObject).get(key); + } else { + return dataObject; + } + } + return dataObject; + } }