JSON 경로가 / 없이 와도 필드 제거·설정이 되도록 수정
- mergeAtRoot / setAt 에서 "encrypted_data" 처럼 선행 / 가 없는 경로를 "/encrypted_data" 로 맞춤 (substring(0, -1) 예외로 병합이 조용히 실패하던 문제) - mergeAtRoot 실패 시 원본을 반환하던 catch 에 WARN 로그 추가
This commit is contained in:
@@ -3,6 +3,7 @@ package com.eactive.eai.util;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import com.eactive.eai.common.util.JacksonUtil;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
@@ -21,6 +22,19 @@ public class JsonPathUtil {
|
||||
// 기본 ObjectMapper 는 100000000.00 을 1.0E8 로 바꿔버린다.
|
||||
private static final ObjectMapper objectMapper = JacksonUtil.newNumberSafeMapper();
|
||||
|
||||
private static final Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
|
||||
/**
|
||||
* 경로를 "/" 로 시작하도록 맞춘다. ("encrypted_data" → "/encrypted_data")
|
||||
* 부모 경로·필드명 계산이 선행 "/" 를 전제로 하므로, 없으면 substring(0, -1) 로 실패한다.
|
||||
*/
|
||||
private static String normalizePath(String path) {
|
||||
if (path == null || path.startsWith("/")) {
|
||||
return path;
|
||||
}
|
||||
return "/" + path;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
// JsonNode 기반 단일 파싱 API — 연속 get/set 시 파싱 횟수 절감
|
||||
// ---------------------------------------------------------------
|
||||
@@ -53,6 +67,7 @@ public class JsonPathUtil {
|
||||
|
||||
/** 이미 파싱된 트리의 경로에 값을 설정 (in-place). */
|
||||
public static void setAt(JsonNode root, String path, String value) {
|
||||
path = normalizePath(path);
|
||||
JsonNode parent = getParentNode(root, getParentPath(path));
|
||||
if (parent == null) return;
|
||||
String key = getFieldName(path);
|
||||
@@ -159,7 +174,8 @@ public class JsonPathUtil {
|
||||
|
||||
ObjectNode result = (ObjectNode) bodyNode.deepCopy();
|
||||
|
||||
// removeFromPath 필드 제거
|
||||
// removeFromPath 필드 제거 ("encrypted_data" 처럼 "/" 없이 와도 같은 경로로 본다)
|
||||
removeFromPath = normalizePath(removeFromPath);
|
||||
int lastSlash = removeFromPath.lastIndexOf('/');
|
||||
String fieldName = removeFromPath.substring(lastSlash + 1);
|
||||
if (lastSlash == 0) {
|
||||
@@ -176,6 +192,8 @@ public class JsonPathUtil {
|
||||
// return objectMapper.writeValueAsString(result);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
// 병합 실패 시 원본을 그대로 돌려주므로, 원인이 묻히지 않게 남긴다.
|
||||
logger.warn("JsonPathUtil] mergeAtRoot failed. return original body. removeFromPath=" + removeFromPath, e);
|
||||
return bodyNode;
|
||||
}
|
||||
}
|
||||
@@ -199,6 +217,7 @@ public class JsonPathUtil {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(mergeAtRoot(objectMapper.readTree(body), removeFromPath, mergeJson));
|
||||
} catch (Exception e) {
|
||||
logger.warn("JsonPathUtil] mergeAtRoot failed. return mergeJson. removeFromPath=" + removeFromPath, e);
|
||||
return mergeJson;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user