Java

Jackson — Annotations (rename, ignore, etc)

admin by @admin ADMIN
2d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Field-level annotations let you map between snake_case JSON and camelCase Java, skip nulls, never serialize secrets, set defaults on missing fields.
Java
Raw
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiUser {
    @JsonProperty("user_id")                       // map "user_id" ↔ userId
    private long userId;

    @JsonProperty("display_name")
    private String displayName;

    @JsonIgnore                                    // never appears in JSON
    private String passwordHash;

    @JsonProperty(defaultValue = "0")
    private int loginCount;

    @JsonProperty("created_at")
    private String createdAt;

    // Constructor for deserialization
    @JsonCreator
    public ApiUser(
        @JsonProperty("user_id")      long userId,
        @JsonProperty("display_name") String displayName) {
        this.userId = userId;
        this.displayName = displayName;
    }
    // getters / setters omitted
}

// Module config example
class Demo {
    static final ObjectMapper MAPPER = new ObjectMapper()
        .configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.