Java应用中带占位符的JSON对象运行时替换库咨询
问题场景
我在Java应用中有如下场景:需要创建带有占位符的JSON对象,如下JSON中的Attribute1和Attribute3需在拥有占位符注册表的其他微服务中解析。
{ "Attribute1": "myPlaceholder1", "Attribute2": "Value of Attribute 2", "Attribute3": "myPlaceholder2" }
我拥有存储对应占位符替换值的注册表:
| Place holder id | Place holder value |
|---|---|
| myPlaceholder1 | U1 |
| myPlaceholder2 | U2 |
请问是否存在可定义占位符并在运行时替换值的Java/JSON库?
可行解决方案
下面几个Java生态中的工具组合或库可以满足你的需求:
Jackson + 自定义占位符遍历替换
Jackson是Java主流JSON处理库,结合它的树模型可以灵活处理任意结构的JSON:- 将原始JSON解析为
JsonNode对象; - 递归遍历所有字符串节点,匹配注册表中的占位符ID并替换;
- 最后将修改后的节点序列化为JSON字符串。
核心代码示例:
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import java.util.Iterator; import java.util.Map; public class PlaceholderReplacer { private static final ObjectMapper mapper = new ObjectMapper(); public static String replaceJsonPlaceholders(String jsonStr, Map<String, String> registry) throws Exception { JsonNode rootNode = mapper.readTree(jsonStr); traverseAndReplace(rootNode, registry); return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode); } private static void traverseAndReplace(JsonNode node, Map<String, String> registry) { if (node.isObject()) { ObjectNode objectNode = (ObjectNode) node; Iterator<Map.Entry<String, JsonNode>> fields = objectNode.fields(); while (fields.hasNext()) { Map.Entry<String, JsonNode> entry = fields.next(); JsonNode value = entry.getValue(); if (value.isTextual()) { String placeholder = value.asText(); if (registry.containsKey(placeholder)) { objectNode.put(entry.getKey(), registry.get(placeholder)); } } else { traverseAndReplace(value, registry); } } } else if (node.isArray()) { for (int i = 0; i < node.size(); i++) { traverseAndReplace(node.get(i), registry); } } } }- 将原始JSON解析为
Apache Commons Text + Jackson
如果你希望用更简洁的字符串替换方式,可以借助Apache Commons Text的StringSubstitutor:- 把原始JSON当作字符串模板,直接用
StringSubstitutor传入注册表完成替换; - 再用Jackson验证替换后的JSON格式正确性。
核心代码示例:
import org.apache.commons.text.StringSubstitutor; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.HashMap; import java.util.Map; public class SimplePlaceholderReplacer { public static void main(String[] args) throws Exception { Map<String, String> registry = new HashMap<>(); registry.put("myPlaceholder1", "U1"); registry.put("myPlaceholder2", "U2"); String jsonTemplate = "{ \"Attribute1\": \"myPlaceholder1\", \"Attribute2\": \"Value of Attribute 2\", \"Attribute3\": \"myPlaceholder2\" }"; StringSubstitutor substitutor = new StringSubstitutor(registry); String replacedJson = substitutor.replace(jsonTemplate); // 验证并输出格式化后的JSON ObjectMapper mapper = new ObjectMapper(); System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(mapper.readTree(replacedJson))); } }- 把原始JSON当作字符串模板,直接用
模板引擎(Freemarker/Thymeleaf)
如果你的JSON是预先定义的模板文件,用模板引擎生成最终JSON更直观:- 将JSON写成模板,占位符使用引擎语法(比如Freemarker的
${myPlaceholder1}); - 传入注册表数据渲染模板,直接得到替换后的JSON字符串。
- 将JSON写成模板,占位符使用引擎语法(比如Freemarker的
这些方案各有侧重:Jackson自定义逻辑适合复杂JSON结构,灵活性最高;Apache Commons Text的方式代码量最少,适合简单场景;模板引擎则适合需要频繁修改JSON模板的场景。
内容的提问来源于stack exchange,提问作者Abdullah Chougle
相关产品推荐
相关产品推荐

