Jackson — a library for converting Java objects to JSON and back. Standard in Spring Boot (default).
Simple analogy: Jackson is like a skilled translator at the United Nations. It sits between two parties who speak different languages — Java objects and JSON strings — and translates between them flawlessly. When you need to send a Java object to a client (JavaScript, mobile app, another service), Jackson "translates" it into JSON. When you receive JSON from an API call, Jackson "translates" it back into a Java object you can work with. Without this translator, you would have to manually parse every field — imagine writing a hundred lines of code to read each JSON key and assign it to the right field.
Why it matters: Almost every modern application communicates via REST APIs using JSON. Jackson is the bridge that makes this communication seamless. Spring Boot uses Jackson by default — when you annotate a class with @RestController and return an object, Spring automatically calls Jackson to serialize it to JSON. Understanding Jackson means understanding how data flows through your application.
Basic usage:
1ObjectMapper mapper = new ObjectMapper();23// Java → JSON4User user = new User(1L, "Alice", "alice@test.com");5String json = mapper.writeValueAsString(user);6// {"id":1,"name":"Alice","email":"alice@test.com"}78// JSON → Java9User parsed = mapper.readValue(json, User.class);1011// Write to a file12mapper.writeValue(new File("user.json"), user);1314// Read from a file15User fromFile = mapper.readValue(new File("user.json"), User.class);1617// JSON to a List18List<User> users = mapper.readValue(19 "[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]",20 new TypeReference<List<User>>() {}21);
Jackson Annotations:
1public class User {2 @JsonProperty("user_id") // Field name in JSON3 private Long id;45 private String name;67 @JsonIgnore // Do not include in JSON8 private String password;910 @JsonFormat(pattern = "dd.MM.yyyy") // Date format11 private LocalDate birthday;1213 @JsonInclude(JsonInclude.Include.NON_NULL) // Do not include null14 private String nickname;1516 @JsonAlias({"user_name", "uname"}) // Accept different names17 public void setName(String name) { this.name = name; }1819 @JsonGetter("full_identity") // Custom getter name in JSON20 public String getFullIdentity() {21 return id + " - " + name;22 }2324 @JsonSetter("uid") // Accept different name for setter25 public void setId(Long id) { this.id = id; }26}
Handling polymorphism:
1@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")2@JsonSubTypes({3 @JsonSubTypes.Type(value = Cat.class, name = "cat"),4 @JsonSubTypes.Type(value = Dog.class, name = "dog")5})6public abstract class Animal {7 private String name;8}910// JSON: {"type":"cat","name":"Whiskers","indoor":true}11Animal animal = mapper.readValue(json, Animal.class); // Returns Cat instance
Custom deserializer:
1public class MoneyDeserializer extends JsonDeserializer<Money> {2 @Override3 public Money deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {4 String value = p.getText(); // "100.50 USD"5 String[] parts = value.split(" ");6 return new Money(new BigDecimal(parts[0]), Currency.getInstance(parts[1]));7 }8}910@JsonDeserialize(using = MoneyDeserializer.class)11private Money amount;
Common mistakes:
@JsonIgnore on sensitive fields — passwords or tokens leak into API responses.User has a List<Order>, each Order references User. Jackson throws StackOverflowError. Fix with @JsonManagedReference / @JsonBackReference or @JsonIgnoreProperties("orders")."2024-01-15" but @JsonFormat expects "dd.MM.yyyy". Always align format strings.TypeReference for generics — mapper.readValue(json, List.class) loses type info and returns List<LinkedHashMap> instead of List<User>.Performance considerations:
ObjectMapper — it is thread-safe and expensive to create. Never instantiate one per request.ObjectMapper.registerModule().readValue on large streams — use JsonParser for streaming processing of huge JSON files.Useful Spring Boot features:
spring.jackson.date-format=dd.MM.yyyy — global date format.spring.jackson.serialization.write-dates-as-timestamps=false.spring.jackson.default-property-inclusion=non_null.spring.jackson.serialization.fail-on-empty-beans=false — avoid errors for empty DTOs.