Repository navigation
Jackson
The jackson feature makes a generated DTO bind to and from JSON without a custom deserializer or a mutable
intermediate type.
modelmaker {
features {
jackson {
enabled = true
includeNonNull = false // default
}
}
}-
@JsonIgnoreProperties(ignoreUnknown = true)- on the class. Always, including nested classes. -
@JsonPropertyOrder({...})- on the class, only when the type has properties. The order is the schema's. -
@JsonCreator- on the all-args constructor, marking it the binding entry point. -
@JsonProperty("<key>")- on every constructor parameter, with the original schema key. -
@JsonProperty("<key>")- on the getters or the fields, followingannotateGetters/annotateFields. -
@JsonInclude(JsonInclude.Include.NON_NULL)- on the class, only withincludeNonNull = true.
Off by default. With jackson { includeNonNull = true } the generated class carries:
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public final class Customer {so an optional property left unset is omitted from the serialized JSON instead of written as "nickname": null.
Nested generated classes get it too. It affects serialization only - deserialization is unchanged, and a property
with a schema default is never null in the first place.
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonPropertyOrder({"id", "first_name"})
public final class Person {
@JsonCreator
Person(
@JsonProperty("id") String id,
@JsonProperty("first_name") @Nullable String firstName) {
this.id = id;
this.firstName = firstName;
}
@JsonProperty("first_name")
public @Nullable String getFirstName() {
return firstName;
}
}Without jackson the all-args constructor is private - the builder is the only way in. With jackson on it becomes
package-private, so Jackson binds straight through it:
// jackson off
private Person(String id, @Nullable String firstName) { ... }
// jackson on
@JsonCreator
Person(@JsonProperty("id") String id, @JsonProperty("first_name") @Nullable String firstName) { ... }Binding goes through the constructor, not the builder, which means:
- The type stays immutable - no setters are needed or generated.
- Schema
defaultvalues apply to deserialized instances too, because they are applied in the constructor. - Required properties are not enforced. A required property missing from the JSON arrives as
null.
Important
A successfully deserialized DTO is not a valid DTO. Jackson binds whatever the JSON contains: a missing
required property becomes null, and @Pattern, @Size, @Min and friends are inert annotations as far as
Jackson is concerned. Validating the result is the consumer's job.
Nothing in the generated code runs a Validator, and neither does Jackson. Something has to do it:
Customer customer = objectMapper.readValue(json, Customer.class); // binding only
Set<ConstraintViolation<Customer>> violations = validator.validate(customer);
if (!violations.isEmpty()) {
throw new IllegalArgumentException(violations.toString());
}Automatic validation at the edge of an API is a framework feature, not a ModelMaker or Jackson one. In Spring
Boot, for instance, a @RequestBody is validated only when the parameter is annotated and an implementation such as
Hibernate Validator is on the classpath:
@PostMapping("/customers")
public ResponseEntity<Void> create(@Valid @RequestBody Customer customer) { ... }Outside such a framework - a Kafka consumer, a batch job, a file reader, a plain ObjectMapper call - no validation
happens unless you invoke it yourself. See Validation.
The JSON key in the schema is what goes into @JsonProperty, even when the Java field name differs:
"first_name": { "type": "string" }private final @Nullable String firstName; // Java name
@JsonProperty("first_name") // wire nameThe rules are on Property Naming.
| Schema type | JSON |
|---|---|
string |
string |
integer / number
|
number |
boolean |
boolean |
bytes |
base64 string |
array |
array |
object / $ref to a generated type |
object |
$ref to a JDK type |
whatever your ObjectMapper does with it |
A $ref to something like java.time.Instant is opaque to ModelMaker - registering the right module (e.g.
jackson-datatype-jsr310) is on you.
ModelMaker emits the annotations but brings no dependencies:
dependencies {
implementation(platform("com.fasterxml.jackson:jackson-bom:2.22.2"))
implementation("com.fasterxml.jackson.core:jackson-databind")
}Annotations are imported from com.fasterxml.jackson.annotation.*, which is the annotation package for both
Jackson 2 and Jackson 3, so the generated code compiles against either:
dependencies {
implementation(platform("tools.jackson:jackson-bom:3.2.2"))
implementation("tools.jackson.core:jackson-databind")
}By default @JsonProperty sits on the getters. Move or duplicate it onto the fields with annotateFields, see
Feature Configuration.
jackson pairs naturally with validation (bind, then validate the bound object) and
openApi (the same property described for API docs). Annotation order on a member is stable: OpenAPI,
Jackson, validation.
ModelMaker - Apache License 2.0 - Issues - Wiki index
Start here
Schemas
Output
Features
Integration
Reference