转换示例:
{“retCode#int”: “code”, “parkInfo#data”: [{“dataType#int”: “DATA_TYPE”, “parkingId#int”: “PARKING_ID”, “realBerth#int”: “REAL_BERTH”, “address#string”: “ADDRESS”, “markExpiry#int”: “MARK_EXPIRY”, “monthBerth#int”: “MONTH_BERTH”, “totalBerth#int”: “TOTLA_BERTH”, “dataTime#string”: “DATA_TIME”, “batteryBerth#int”: “BATTERY_BERTH”, “leaderTel#string”: “LEADER_TEL”, “nobarryBerth#int”: “NOBARRY_BERTH”, “onlinePay#string”: “ONLINE_PAY”, “serverTel#string”: “SERVER_TEL”, “districtId#string”: “DISTRICT_ID”, “leaderName#string”: “LEADER_NAME”, “parkingStatus#int”: “PARKING_STATUS”, “recodeMark#string”: “RECORD_MARK”, “rentExpiry#string”: “RENT_EXPIRY”, “serverTime#string”: “SERVER_TIME”, “serverType#string”: “SERVER_TYPE”, “supplierId#string”: “SUPPLIER_ID”, “transferBerth#int”: “TRANSFER_BERTH”, “updateTime#string”: “UPDATE_TIME”, “estatesName#string”: “ESTATES_NAME”, “parkingArea#string”: “PARKING_AREA”, “parkingName#string”: “PARKING_NAME”, “manageCodeId#string”: “MANAGECODE_ID”, “companyManage#string”: “COMPANY_MANAGE”, “complainedTel#string”: “COMPLAINED_TEL”, “parkingNature#string”: “PARKING_NATURE”, “cityCode#string#const”: “310000”, “parkingEstates#string”: “PARKING_ESTATES”, “parkingProperty#string”: “PARKING_PROPERTY”}], “retMsg#string”: “message”}
/**
-
@author chenxiaoyang
-
@description: 字段转换工具
-
@dATE:2021/12/8 18:45
*/
public class ProtocolConvertUtil {private Logger log = LoggerFactory.getLogger(ProtocolConvertUtil.class);
@Autowired
DataTypeManageContext dataTypeManageContext;@Autowired
ComponentConfigs componentConfigs;/*
- 转换参数,组合参数赋值结果
*/
public Map<String, Object> convertParam(Map<String, Object> sourceParam,
Map<String, Object> interfaceParamConfigMap) {
Map<String, Object> result = new HashMap<>();
//遍历替换值
for (Map.Entry<String, Object> m : interfaceParamConfigMap.entrySet()) {
String paramKey = m.getKey();
Object sourceField = m.getValue();
createValue(sourceParam, paramKey, result, sourceField);
}
return result;
}
/*
- 组装值
*/
public void createValue(Map<String, Object> sourceParam, String paramKey, Map<String, Object> result,
Object sourceField) {
String[] paramKeys = paramKey.split(“#”);
//不带#可能是map或者List,直接组装值
if (sourceField instanceof String) {
//如果paramKeys长度为3,那可能是特殊的值,目前仅考虑常量const
String valueType = paramKeys[1];
ConvertData convertData = dataTypeManageContext.getDataBaseUtil(valueType);
Object obj = null;
if (paramKeys.length == 2) {
//数据转换
obj = getSingleValue(sourceParam, (String) sourceField);
result.put(paramKeys[0], obj != null ? convertData.convertValue(obj) : null);
} else if (paramKeys.length == 3 && “const”.equals(paramKeys[2])) {
obj = sourceField;
result.put(paramKeys[0], obj != null ? convertData.convertValue(obj) : null);
}
} else if (sourceField instanceof Map) {
String sourceKey = paramKeys[1];
Map<String, Object> m = new HashMap<>();
for (Map.Entry<String, Object> s : ((Map<String, Object>) sourceField).entrySet()) {
String sKey = s.getKey();
Object sField = s.getValue();
createValue((Map<String, Object>) sourceParam.get(sourceKey), sKey, m, sField);
}
result.put(paramKeys[0], m);
} else if (sourceField instanceof List) {
String sourceKey = paramKeys[1];
//获取源数据里的List进行遍历
Map<String, Object> sourceMapping = (Map<String, Object>) ((List) sourceField).get(0);
//组装List的值
List<Map<String, Object>> mapList = createListDataValue(sourceMapping, sourceParam, sourceKey);
result.put(paramKeys[0], mapList);
}
}
/*
- 组装List的值
*/
public List<Map<String, Object>> createListDataValue(Map<String, Object> sourceMapping, Map<String, Object> sourceParam,
String sourceKey) {
//组装List值用的
List<Map<String, Object>> mapList = new ArrayList<>();
List<Map<String, Object>> sourceDataList = (List<Map<String, Object>>) sourceParam.get(sourceKey);
if (CollectionUtils.isEmpty(sourceDataList)) return null;
for (Map<String, Object> a : sourceDataList) {
Map<String, Object> v = new HashMap<>();
createListInnerData(v, sourceMapping, a);
mapList.add(v);
}
return mapList;
}
public void createListInnerData(Map<String, Object> v, Map<String, Object> sourceMapping,
Map<String, Object> data) {
for (Map.Entry<String, Object> c : sourceMapping.entrySet()) {
String sKey = c.getKey();
Object sField = c.getValue();
createValue(data, sKey, v, sField);
}
}/*
- 组装最终的值
*/
public Object getSingleValue(Map<String, Object> sourceParam, String sourceField) {
Object obj = sourceParam.get(sourceField);
return obj;
}
public InterfaceBaseConfig getInterfaceBaseConfig(String interfaceBusinessId) {
InterfaceBaseConfig interfaceBaseConfig = componentConfigs.interfaceBaseConfigMap.get(interfaceBusinessId);
return interfaceBaseConfig;
}public Map<String, Object> getInterfaceParamConfigMap(int paramType, int interfaceId) {
Map<String, Object> interfaceParamConfigMap = null;
if (paramType == ParamTypeEnum.INPUT.code) {
interfaceParamConfigMap = componentConfigs.interfaceInputJsonParamConfigMap.get(interfaceId);
} else {
interfaceParamConfigMap = componentConfigs.interfaceOutputJsonParamConfigMap.get(interfaceId);
}
return interfaceParamConfigMap;
}/*
- 根据JSON进行数据转换
*/
public Map<String, Object> convertJsonParamForInputParam(Map<String, Object> sourceParam,
String interfaceBusinessId) {
try {
InterfaceBaseConfig interfaceBaseConfig = getInterfaceBaseConfig(interfaceBusinessId);
if (interfaceBaseConfig == null) return null;
Map<String, Object> interfaceParamConfigMap = getInterfaceParamConfigMap(ParamTypeEnum.INPUT.code,
interfaceBaseConfig.getId());
return convertParam(sourceParam, interfaceParamConfigMap);
} catch (Exception e) {
log.error(“转换错误:{}”, e);
throw e;
}
}
/*
- 根据JSON进行数据转换
*/
public Map<String, Object> convertJsonParamForOutputParam(Map<String, Object> sourceParam,
String interfaceBusinessId) {
try {
InterfaceBaseConfig interfaceBaseConfig = getInterfaceBaseConfig(interfaceBusinessId);
if (interfaceBaseConfig == null) return null;
Map<String, Object> interfaceParamConfigMap = getInterfaceParamConfigMap(ParamTypeEnum.OUTPUT.code,
interfaceBaseConfig.getId());
return convertParam(sourceParam, interfaceParamConfigMap);
} catch (Exception e) {
log.error(“转换错误:{}”, e);
throw e;
}
}
/*
- 根据JSON进行数据转换
*/
public void convertToProtoBufForJsonInputParam(Map<String, Object> sourceParam,
String interfaceBusinessId, Message.Builder message) {
try {
InterfaceBaseConfig interfaceBaseConfig = getInterfaceBaseConfig(interfaceBusinessId);
if (interfaceBaseConfig == null) return;
Map<String, Object> interfaceParamConfigMap = getInterfaceParamConfigMap(ParamTypeEnum.INPUT.code,
interfaceBaseConfig.getId());
Map<String, Object> paramMap = convertParam(sourceParam, interfaceParamConfigMap);
JsonUtil.mapToProtoBuf(paramMap, message);
} catch (Exception e) {
log.error(“转换错误:{}”, e);
throw e;
}
}
/*
- 根据JSON进行数据转换
*/
public void convertToProtoBufForJsonOutputParam(Map<String, Object> sourceParam,
String interfaceBusinessId, Message.Builder message) {
try {
InterfaceBaseConfig interfaceBaseConfig = getInterfaceBaseConfig(interfaceBusinessId);
if (interfaceBaseConfig == null) return;
Map<String, Object> interfaceParamConfigMap = getInterfaceParamConfigMap(ParamTypeEnum.OUTPUT.code,
interfaceBaseConfig.getId());
Map<String, Object> paramMap = convertParam(sourceParam, interfaceParamConfigMap);
JsonUtil.mapToProtoBuf(paramMap, message);
} catch (Exception e) {
log.error(“转换错误:{}”, e);
throw e;
}
}
/*
- 根据JSON进行数据转换
*/
public Map<String, Object> convertJsonParamForInputParam(String sourceJson,
String interfaceBusinessId) {
try {
Map<String, Object> sourceParam = Json.toHashMap(sourceJson);
InterfaceBaseConfig interfaceBaseConfig = getInterfaceBaseConfig(interfaceBusinessId);
if (interfaceBaseConfig == null) return null;
Map<String, Object> interfaceParamConfigMap = getInterfaceParamConfigMap(ParamTypeEnum.INPUT.code,
interfaceBaseConfig.getId());
return convertParam(sourceParam, interfaceParamConfigMap);
} catch (Exception e) {
log.error(“转换错误:{}”, e);
throw e;
}
}
/*
- 根据JSON进行数据转换
*/
public Map<String, Object> convertJsonParamForOutputParam(String sourceJson,
String interfaceBusinessId) {
try {
Map<String, Object> sourceParam = Json.toHashMap(sourceJson);
InterfaceBaseConfig interfaceBaseConfig = getInterfaceBaseConfig(interfaceBusinessId);
if (interfaceBaseConfig == null) return null;
Map<String, Object> interfaceParamConfigMap = getInterfaceParamConfigMap(ParamTypeEnum.OUTPUT.code,
interfaceBaseConfig.getId());
return convertParam(sourceParam, interfaceParamConfigMap);
} catch (Exception e) {
log.error(“转换错误:{}”, e);
throw e;
}
}
/*
- 根据JSON进行数据转换
*/
public void convertToProtoBufForJsonInputParam(String sourceJson, String interfaceBusinessId,
Message.Builder message) {
try {
Map<String, Object> sourceParam = Json.toHashMap(sourceJson);
InterfaceBaseConfig interfaceBaseConfig = getInterfaceBaseConfig(interfaceBusinessId);
if (interfaceBaseConfig == null) return;
Map<String, Object> interfaceParamConfigMap = getInterfaceParamConfigMap(ParamTypeEnum.INPUT.code,
interfaceBaseConfig.getId());
Map<String, Object> paramMap = convertParam(sourceParam, interfaceParamConfigMap);
JsonUtil.mapToProtoBuf(paramMap, message);
} catch (Exception e) {
log.error(“转换错误:{}”, e);
throw e;
}
}
/*
- 根据JSON进行数据转换
*/
public void convertToProtoBufForJsonOutputParam(String sourceJson, String interfaceBusinessId,
Message.Builder message) {
try {
Map<String, Object> sourceParam = Json.toHashMap(sourceJson);
InterfaceBaseConfig interfaceBaseConfig = getInterfaceBaseConfig(interfaceBusinessId);
if (interfaceBaseConfig == null) return;
Map<String, Object> interfaceParamConfigMap = getInterfaceParamConfigMap(ParamTypeEnum.OUTPUT.code,
interfaceBaseConfig.getId());
Map<String, Object> paramMap = convertParam(sourceParam, interfaceParamConfigMap);
JsonUtil.mapToProtoBuf(paramMap, message);
} catch (Exception e) {
log.error(“转换错误:{}”, e);
throw e;
}
}
- 转换参数,组合参数赋值结果
// public void main(String[] args) {
// String interfaceString = “{\n” +
// ““retCode#int”: “code”,\n” +
// ““retMsg#string”: “message”,\n” +
// ““lineNo#string”:“lineNo”,\n” +
// ““data#datas”:[\n” +
// “{\n” +
// ““stationRunTimeModelList#stationRunTimeModelList”:[{\n” +
// " “stationId#string”:“stationId”,\n" +
// " “stationName#string”:“stationName”,\n" +
// ““upStationDetails#upStationDetailList”:[\n” +
// “{\n” +
// " “arriveStation#string”:“arriveStation”,\n" +
// " “arriveTime#string”:“arriveTime”,\n" +
// " “endStation#string”:“endStation”,\n" +
// " “endStationId#string”:“endStationId”,\n" +
// " “trainId#string”:“trainId”\n" +
// “}\n” +
// “],\n” +
// ““downStationDetails#downStationDetailList”:[{\n” +
// " “arriveStation#string”:“arriveStation”,\n" +
// " “arriveTime#string”:“arriveTime”,\n" +
// " “endStation#string”:“endStation”,\n" +
// " “endStationId#string”:“endStationId”,\n" +
// " “trainId#string”:“trainId”\n" +
// “}\n” +
// “]\n” +
// “}]\n” +
// “}]\n” +
// “\n” +
// “}”;
// String data = “{“code”:“1000”,“message”:“success”,“datas”:[{“lineNo”:“01”,“stationRunTimeModelList”:[{“stationId”:“0111”,“stationName”:“莘庄”,“transferId”:”“,“openDoor”:”“,“upStationDetailList”:[],“downStationDetailList”:[{“arriveStation”:”“,“arriveTime”:“2022-01-14 19:02:51”,“endStation”:“莘庄”},{“arriveStation”:”“,“arriveTime”:“2022-01-14 19:04:35”,“endStation”:“莘庄”},{“arriveStation”:”“,“arriveTime”:“2022-01-14 19:07:08”,“endStation”:“莘庄”}]},{“stationId”:“0112”,“stationName”:“外环路”,“transferId”:”“,“openDoor”:”“,“upStationDetailList”:[{“arriveStation”:”“,“arriveTime”:“2022-01-14 19:00:32”,“endStation”:“富锦路”}],“downStationDetailList”:[{“arriveStation”:”“,“arriveTime”:“2022-01-14 19:01:45”,“endStation”:“莘庄”},{“arriveStation”:”“,“arriveTime”:“2022-01-14 19:04:18”,“endStation”:“莘庄”},{“arriveStation”:”“,“arriveTime”:“2022-01-14 19:10:32”,“endStation”:“莘庄”}] }]}]}”;
// Map<String, Object> sourceParam = Json.toHashMap(data);
// Map<String, Object> interfaceParamConfigMap = Json.toHashMap(interfaceString);
// FlowMap result = convertParamToFlowMap(sourceParam,
// interfaceParamConfigMap);
// List list=result.nlm(“data”);
// System.out.println(Json.toJson(list));
// }
}