JSON mapping with Jackson
Control exactly what your API emits: dates, nulls, field names and secrets.
Open this lesson in the learning hubKey points
- Boot auto-configures one
ObjectMapper, registers the JavaTimeModule, and writes dates as ISO-8601 text. - Tune it globally with
spring.jackson.*, or add aJackson2ObjectMapperBuilderCustomizerbean. @JsonPropertyrenames a field on the wire; a naming strategy switches the whole API to snake_case.@JsonInclude(NON_NULL)omits empty fields instead of shipping a wall ofnull.@JsonIgnorehides a field, but a DTO that never had it is safer - you cannot forget the annotation.- Records bind through their canonical constructor, and unknown JSON fields are ignored by default in Boot.
Example
public record OrderResponse(
long id,
@JsonProperty("customer_email") String customerEmail, // renamed on the wire
BigDecimal total, // 42.00, a JSON number
@JsonInclude(JsonInclude.Include.NON_NULL)
BigDecimal discount, // key omitted when null
LocalDateTime createdAt) { // "2026-05-01T10:00:00"
}
// An entity should never be the response type. If you must expose one, hide the secrets:
@Entity
class User {
@JsonIgnore
private String passwordHash;
}
// The same choices as properties, in application.yml:
// spring.jackson.default-property-inclusion: non_null
// spring.jackson.property-naming-strategy: SNAKE_CASE
// spring.jackson.serialization.write-dates-as-timestamps: false
The JSON your API emits is a contract - decide it deliberately instead of inheriting it from your entities.
This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Spring Boot course, and every lesson in it is listed on the Spring Boot contents page.