JSON Processing in Java
JSON is the standard data format for REST APIs. Java developers use Jackson (default in Spring Boot) or Gson to convert between Java objects and JSON strings.
Jackson with Spring Boot
// Spring Boot auto-configures Jackson
// Request body automatically deserialized:
@PostMapping("/users")
public User create(@RequestBody User user) {
return userService.save(user);
}
// Manual serialization:
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(user);
User parsed = mapper.readValue(json, User.class);Common Jackson Annotations
- @JsonProperty("full_name") — Custom JSON field name.
- @JsonIgnore — Exclude field from serialization.
- @JsonFormat(pattern = "yyyy-MM-dd") — Date formatting.
- @JsonInclude(Include.NON_NULL) — Skip null fields.
Handling Dates
Use ISO-8601 format (2025-06-13T10:30:00Z) for API dates. Register JavaTimeModule for Java 8 date/time types. Configure ObjectMapper globally in Spring Boot via application.yml.
Frequently Asked Questions
Jackson vs Gson?▼
Jackson is the Spring Boot default, faster, and more feature-rich. Gson is simpler for standalone apps. Use Jackson for Spring projects.
