Master Dynamic JSON in Spring Boot 3: 4 Practical Techniques
This article explains why Spring Boot 3 uses Jackson for JSON handling, describes four effective methods—JsonNode, Map, @JsonAnySetter, and custom deserializer—to process dynamic JSON properties, and promotes a continuously updated collection of over 90 Spring Boot case studies with source code.
Spring Boot 3 uses Jackson for JSON handling, allowing easy serialization and deserialization. When JSON contains dynamic properties not defined in the POJO, several strategies can be applied.
1. Introduction
Jackson handles predefined JSON structures automatically, but dynamic attributes require special techniques.
2. Practical Cases
2.1 Use JsonNode property
Define a JsonNode field in the POJO to capture the dynamic part.
<code>public class Product {
// other properties
private JsonNode details;
// getters, setters
}</code>Test code:
<code>ObjectMapper objectMapper = new ObjectMapper();
String json = "{ \"name\": \"SpringBoot实战案例100例\", \"category\": \"book\", \"details\": { \"price\": \"70\", \"author\": \"XGPack\" } }";
Product product = objectMapper.readValue(json, Product.class);
System.err.printf("name: %s, category: %s%n", product.getName(), product.getCategory());
System.out.println("--------------------------------");
System.err.printf("price: %s, author: %s%n",
product.getDetails().get("price").asText(),
product.getDetails().get("author").asText());
</code>Output:
2.2 Use Map collection
Store dynamic attributes in a Map<String,Object> field.
<code>public class Product_Map {
// other properties
private Map<String, Object> details;
// getters, setters
}</code>Test code:
<code>ObjectMapper objectMapper = ...;
String json = ...;
Product product = objectMapper.readValue(json, Product.class);
System.err.printf("name: %s, category: %s%n", product.getName(), product.getCategory());
System.out.println("--------------------------------");
System.err.printf("price: %s, author: %s%n",
product.getDetails().get("price"),
product.getDetails().get("author"));
</code>2.3 Use @JsonAnySetter
Annotate a setter method to capture any unknown properties into a map.
<code>public class Product {
// other properties
private Map<String, Object> details = new LinkedHashMap<>();
@JsonAnySetter
public void setDetail(String key, Object value) {
details.put(key, value);
}
// getters, setters
}</code>Test code similar to previous examples; output shown below.
2.4 Custom deserializer
Implement a StdDeserializer to manually parse known and dynamic fields.
<code>public class ProductDeserializer extends StdDeserializer<Product> {
public ProductDeserializer() { this(null); }
public ProductDeserializer(Class<?> vc) { super(vc); }
@Override
public Product deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
JsonNode node = jp.getCodec().readTree(jp);
String name = node.get("name").asText();
String category = node.get("category").asText();
JsonNode detailsNode = node.get("details");
String price = detailsNode.get("price").asText();
String author = detailsNode.get("author").asText();
Map<String, Object> details = new HashMap<>();
details.put("price", price);
details.put("author", author);
return new Product(name, category, details);
}
}</code>Register the deserializer:
<code>ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Product.class, new ProductDeserializer());
objectMapper.registerModule(module);
</code>Alternatively, annotate the POJO with @JsonDeserialize(using = ProductDeserializer.class) or use @JsonComponent in a Spring Boot application.
The article also announces a Spring Boot 3 case collection containing over 90 practical articles, promising permanent updates and offering source code and detailed MD notes to subscribers.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.