Sub topic 10
Renaming and shaping JSON with Jackson annotations
This chapter assumes you can write a Spring Boot @RestController, you know what a Java record is, and you have seen Jackson turn a record into JSON and back. Here you control the JSON field names and contents without changing the record's component names.
Why shape the JSON
By default, Jackson uses the record component names as JSON property names. That is fine until the API contract differs from your Java naming. A client may expect snake_case names, a field may need to be hidden, or a null value may need to be left out of the output.
Jackson annotations on record components let you adjust the contract. The record stays a plain data carrier, and the JSON stays stable for clients.
Renaming properties with @JsonProperty
@JsonProperty sets the JSON name for a component. On a record, put it on the component itself. Jackson uses it for both reading and writing.
import com.fasterxml.jackson.annotation.JsonProperty;
public record UserResponse(
Long id,
@JsonProperty("full_name") String name,
@JsonProperty("email_address") String email) {
}The record accessors are still name() and email(). Only the JSON uses the new names:
{"id":1,"full_name":"Ada","email_address":"[email protected]"}The same annotation works on request records. A body such as {"full_name":"Ada"} is bound to the name component.
Omitting nulls with @JsonInclude
@JsonInclude controls which values are written. Include.NON_NULL skips components whose value is null. Put it on the record to apply it to every component.
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
public record ProfileResponse(Long id, String name, String nickname) {
}If nickname is null, the output is {"id":1,"name":"Ada"}. Clients must then treat a missing field as "no value".
Hiding and accepting fields
Use @JsonIgnore to keep a component out of the JSON. Use @JsonProperty(access = ...) when a field should travel in only one direction.
import com.fasterxml.jackson.annotation.JsonProperty;
public record LoginRequest(
String username,
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) String password) {
}WRITE_ONLY means the value can be read from incoming JSON but is never written to outgoing JSON. This protects a password if the record is ever serialized, for example in a log. READ_ONLY is the opposite.
Formatting values
@JsonFormat changes how a value is written. It is common for dates.
import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDate;
public record BookingResponse(
Long id,
@JsonFormat(pattern = "dd/MM/yyyy") LocalDate checkIn) {
}Spring Boot registers the Java time module for you, so LocalDate works without extra setup. With the pattern above, the date is written as "01/12/2026".
Choosing between annotations and a global setting
If every field needs snake_case, annotating each component is noisy. Set a global naming strategy in application.properties instead:
spring.jackson.property-naming-strategy=SNAKE_CASEThen use @JsonProperty only for the exceptions.
| Need | Tool |
|---|---|
| Rename one field | `@JsonProperty("name")` |
| Rename all fields the same way | `spring.jackson.property-naming-strategy` |
| Skip null values | `@JsonInclude(NON_NULL)` |
| Never expose a field | `@JsonIgnore` or `WRITE_ONLY` |
| Custom date or number format | `@JsonFormat` |
Common errors
- Renamed field arrives as null. The client still sends the old name. After adding
@JsonProperty("full_name"), the JSON keynameno longer binds. - Validation messages show the Java name. Bean Validation reports the component name (
name), not the JSON name (full_name). Map field errors to JSON names in your error handler if clients need them. - Annotation seems ignored. Check that the import is from
com.fasterxml.jackson.annotation, not another library. - Too much annotation. Heavy JSON tuning on a record can mean the DTO is trying to serve several clients. Consider a separate record for each contract.
Exercise
You add @JsonProperty("full_name") to the name component of CreateUserRequest. A client keeps sending {"name":"Ada"}. What happens to the name component, and how would you fix it without breaking the client?
Show answer
The name component is bound to the JSON key full_name, so the key name is not matched and the component is null. If @NotBlank is present, validation fails. To fix it, either update the client to send full_name, or accept both names by adding @JsonAlias("name") next to @JsonProperty("full_name").