The @JsonProperty annotation tells Jackson which JSON key a Java property maps to. Use it to rename a property, to map snake_case or otherwise differently named JSON onto your fields, to include a private field, and to mark a property as read-only or write-only.
It belongs to the com.fasterxml.jackson.annotation package, and Spring Boot applies it automatically because Jackson is the default JSON library there.
By default Jackson derives names from getters and setters, so a getFirstName() method becomes the key firstName. When an external API uses another convention, put @JsonProperty on the field, getter or setter.
The annotation applies in both directions: the same name is read on input and written on output. Jackson links the field and its getter and setter into one logical property, so annotating one of them is enough.
If every property needs the same conversion, do not annotate each one. Set a naming strategy on the class or the mapper instead:
In Spring Boot you can apply it globally with spring.jackson.property-naming-strategy=SNAKE_CASE.
Jackson only sees public fields and getters or setters by default. @JsonProperty makes a private member visible, which is useful for immutable classes.
@JsonCreator marks the constructor Jackson should use, and @JsonProperty on each parameter names the JSON key that feeds it. If you compile with the -parameters flag and register the jackson-module-parameter-names module (Spring Boot does this), constructor parameter names are detected without annotations.
On a Java record, annotate the components. Jackson 2.12 and later supports records natively, so no @JsonCreator is needed.
The access attribute controls the direction in which a property is processed.
| Access value | Serialization (object to JSON) | Deserialization (JSON to object) |
|---|---|---|
AUTO (default) | Yes | Yes |
READ_ONLY | Yes | Ignored |
WRITE_ONLY | Ignored | Yes |
READ_WRITE | Yes | Yes |
This is the safe way to accept a password in a request body without echoing it in the response.
The attributes required, index and defaultValue are often misunderstood.
required = true is enforced only for creator (constructor or factory) parameters. Jackson then fails with a MismatchedInputException when the key is absent. On a plain field with a setter it has no effect at runtime, although schema generators read it.defaultValue is documentation metadata. Jackson does not apply it when a key is missing, so initialize the field yourself (private double price = 0.0;).index sets the position of a property in the output when a stable order matters. For ordering a whole class use @JsonPropertyOrder({"id", "name"}).null, 0, false).To reject unexpected input, enable DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES. Spring Boot disables it by default, while a bare ObjectMapper enables it.
When an API changes a key over time, you can read both names and still write a single one.
@JsonAlias affects deserialization only, so output always uses the @JsonProperty name.
defaultValue to fill missing values.@JsonProperty to hide a field. Use @JsonIgnore or WRITE_ONLY for that.org.codehaus.jackson package from Jackson 1, which Jackson 2 does not read.@JsonProperty sets the primary name used for both reading and writing. @JsonAlias adds extra names that are accepted only when reading.
No. Jackson maps matching names by default. You only need it for different names, private members without accessors, access modes and constructor parameters.
Yes. Jackson combines the field, getter and setter of a property, so one annotation applies to all of them.
No. Gson uses @SerializedName. @JsonProperty is specific to Jackson.
@JsonProperty maps Java members to JSON keys, exposes private fields and constructor parameters, and sets read-only or write-only behaviour. Remember that defaultValue does not populate anything and required only applies to creators, and prefer a naming strategy or @JsonAlias over repeating annotations when a whole class follows a convention.