CryptoUtils类getKey(String,String)方法未定义问题求助
问题解决:CryptoUtils中getKey方法未定义的错误
问题原因
你提供的CryptoUtils类里,flatMap方法调用了getKey(String, String)方法,但该方法并未在类中实现,导致编译报错。
解决方案
在CryptoUtils类中添加getKey方法的实现。根据代码逻辑,这个方法的作用是拼接嵌套键名(父键与子键用.分隔,若父键为空则直接返回子键)。
修改后的完整代码
在flatMap方法上方添加以下getKey方法:
private static String getKey(String rootKey, String subKey) { if (rootKey == null || rootKey.isEmpty()) { return subKey; } return rootKey + "." + subKey; }
完整的CryptoUtils类代码如下:
package normjsn; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.text.Collator; import java.util.*; public class CryptoUtils { private final static ObjectMapper mapper = new ObjectMapper(); public static byte[] hexStringToByteArray(String s) { int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); } return data; } public static String normalJson(Object object, Map<String, Object> header) { if (object == null && header == null) return null; Map<String, Object> map = null; if (object != null) { if (object instanceof String) { try { object = mapper.readValue((String) object, Object.class); } catch (JsonProcessingException e) { throw new RuntimeException(e.getMessage()); } } if (object instanceof Collection) { /// @SuppressWarnings("rawtypes") /// Skip non generic var s warning PacketsWrapper packetsWrapper = new PacketsWrapper((Collection) object); map = mapper.convertValue(packetsWrapper, Map.class); } else { map = mapper.convertValue(object, Map.class); } } if (map == null && header != null) { map = header; } if (map != null && header != null) { for (Map.Entry<String, Object> entry : header.entrySet()) { map.put(entry.getKey(), entry.getValue()); } } Map<String, Object> result = new HashMap<>(); flatMap(result, null, map); StringBuilder sb = new StringBuilder(); List<String> keys = new ArrayList<>(result.keySet()); Collections.sort(keys, Collator.getInstance(Locale.ENGLISH)); for (String key : keys) { String textValue; Object value = result.get(key); if (value != null) { textValue = value.toString(); if (textValue == null || textValue.equals("")) { textValue = "#"; } else { textValue = textValue.replaceAll("#", "##"); } } else { textValue = "#"; } sb.append(textValue).append('#'); } return sb.deleteCharAt(sb.length() - 1).toString(); } // 新增的getKey方法 private static String getKey(String rootKey, String subKey) { if (rootKey == null || rootKey.isEmpty()) { return subKey; } return rootKey + "." + subKey; } private static void flatMap(Map<String, Object> result, String rootKey, Object input) { if (input instanceof Collection) { Collection list = (Collection) input; int i = 0; for (Object e : list) { String key = getKey(rootKey, "E" + i++); flatMap(result, key, e); } } else if (input instanceof Map) { Map<String, Object> map = (Map) input; for (Map.Entry<String, Object> entry : map.entrySet()) { flatMap(result, getKey(rootKey, entry.getKey()), entry.getValue()); } } else { result.put(rootKey, input); } } private static class PacketsWrapper { private Collection packets; public PacketsWrapper() {} public PacketsWrapper(Collection packets) { this.packets = packets; } public Collection getPackets() { return packets; } public void setPackets(Collection packets) { this.packets = packets; } } }
说明
这个getKey方法的逻辑:
- 若
rootKey为空(顶级键场景),直接返回subKey - 若
rootKey不为空,将父键与子键用.拼接,形成类似user.name、list.E0.id的嵌套键名,符合JSON扁平化处理的常规规则。
内容的提问来源于stack exchange,提问作者nima tehrani
相关产品推荐
相关产品推荐

