Quick revision
The short answer
For an immutable class we tell Jackson which constructor to use: annotate it with @JsonCreator and name each parameter with @JsonProperty("id") and @JsonProperty("name"), or compile with -parameters and register ParameterNamesModule (built into Jackson 3) so the names are found without annotations. A Java record needs nothing at all since Jackson 2.12, because Jackson uses its canonical constructor. Missing properties become null or default values unless FAIL_ON_MISSING_CREATOR_PROPERTIES or FAIL_ON_NULL_CREATOR_PROPERTIES is enabled, and records can validate in their compact constructor.
What to remember
In this answer, we saw that an immutable class without setters fails until Jackson knows its creator. We fixed it with @JsonCreator and @JsonProperty, or with -parameters and ParameterNamesModule, which Spring Boot registers and Jackson 3 builds in. Records work directly since Jackson 2.12 through the canonical constructor, missing properties become null or defaults unless FAIL_ON_MISSING_CREATOR_PROPERTIES or FAIL_ON_NULL_CREATOR_PROPERTIES is on, and compact constructors are a good place for validation.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
For an immutable class we tell Jackson which constructor to use: annotate it with @JsonCreator and name each parameter with @JsonProperty("id") and @JsonProperty("name"), or compile with -parameters and register ParameterNamesModule (built into Jackson 3) so the names are found without annotations. A Java record needs nothing at all since Jackson 2.12, because Jackson uses its canonical constructor. Missing properties become null or default values unless FAIL_ON_MISSING_CREATOR_PROPERTIES or FAIL_ON_NULL_CREATOR_PROPERTIES is enabled, and records can validate in their compact constructor.
Why this question is asked
Immutable DTOs are safer than mutable beans: they cannot be half-built or changed by accident after validation, and they are naturally thread-safe. But Jackson's classic way of building objects is to call a no-argument constructor and then setters, and an immutable class has neither. Interviewers ask this coding question to see whether we know how Jackson builds objects through constructors, called creators, and what happens when data is missing. In this answer, we will see why the plain class fails, fix it with @JsonCreator, then with parameter names, move to a record, handle missing properties, and add validation.
1Why the Plain Immutable Class Fails
Jackson needs a way to create the object. With no default constructor and no setters, and with a constructor whose parameter names Jackson cannot see, it has nothing to work with. Java does not keep parameter names in compiled classes unless we ask the compiler to, so to Jackson the constructor looks like User(long arg0, String arg1).
Notice that in a Spring Boot application this exact class may work, which confuses people. The reason is in the next section: Spring Boot registers the parameter names module and its build compiles with -parameters.
2Fix 1: @JsonCreator With @JsonProperty
A creator is a constructor or static factory method that Jackson calls to build an object, passing JSON values as arguments. @JsonCreator marks which one, and @JsonProperty on each parameter says which JSON field feeds it. This works everywhere, with any compiler flags, so it is the most explicit option.
Jackson now reads the whole JSON object, collects the values for id and name, and calls the constructor once with both. Fields in the JSON that are not constructor parameters can still be set through setters or fields if they exist, but for a fully immutable class everything goes through the creator.
3Fix 2: Parameter Names Without Annotations
If the compiler keeps parameter names, which javac does with the -parameters flag, Jackson can read them through the ParameterNamesModule, from jackson-module-parameter-names in Jackson 2. Then the constructor needs no @JsonProperty on its parameters. Spring Boot registers this module automatically, and the Spring Boot Maven and Gradle setups compile with -parameters, which is why such classes often just work in Boot applications. In Jackson 3, parameter name support is built into databind, so no separate module is needed.
But there is a catch: a single-argument constructor is ambiguous. Jackson cannot be sure whether User(String name) should receive the whole JSON value or the property name, so for one-argument creators we still set the mode explicitly. That ambiguity is the subject of its own debugging story.
4Records Need Nothing
A Java record is a class whose fields, accessors and canonical constructor, the constructor that takes all components in order, are generated by the compiler. Records always keep their component names in the class file, so Jackson knows which JSON field belongs to which parameter without any flag. Since Jackson 2.12, records are supported directly: Jackson serializes them through their accessors and deserializes them through the canonical constructor.
| Shape | What Jackson needs | Works without Spring Boot |
|---|---|---|
| Mutable bean | Default constructor and setters | Yes |
| Immutable class | @JsonCreator plus @JsonProperty per parameter | Yes |
| Immutable class, no annotations | -parameters and ParameterNamesModule | Only with that setup |
| Record | Nothing (Jackson 2.12+) | Yes |
So for new DTOs, a record is the shortest correct answer: no annotations, no module, immutable by design.
5Missing Properties and Validation
What if the JSON has only an id? A creator must be called with a value for every parameter, so Jackson passes a default for the missing ones: null for objects, 0 or false for primitives. By default that happens silently. DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES makes Jackson fail when a creator property is absent, and FAIL_ON_NULL_CREATOR_PROPERTIES makes it fail when one is null, whether missing or explicitly null.
A compact constructor, a record constructor without the parameter list, runs before the fields are assigned and is a natural place for invariants that must always hold. Jackson wraps the exception it throws, and in Spring MVC that becomes a 400 Bad Request. For rich, user-facing error messages, Bean Validation annotations on the record components with @Valid on the controller parameter are usually the better tool, and both can be combined.
6Follow-up questions
Can we use a static factory method instead of a constructor?
Yes. @JsonCreator works on a static method that returns the type, such as User.of(long id, String name), with the same @JsonProperty rules. That is useful when the class wants to normalise input or return cached instances.
What happens to JSON fields that are not constructor parameters?
Jackson tries to set them through setters or fields. If there are none, they are unknown properties, so they fail or are ignored depending on FAIL_ON_UNKNOWN_PROPERTIES and @JsonIgnoreProperties.
Do records work with Lombok or older Jackson versions?
Records need Jackson 2.12 or later; older versions do not understand them. Lombok is not needed for records at all, because the compiler already generates what Lombok would.
7Conclusion
In this answer, we saw that an immutable class without setters fails until Jackson knows its creator. We fixed it with @JsonCreator and @JsonProperty, or with -parameters and ParameterNamesModule, which Spring Boot registers and Jackson 3 builds in. Records work directly since Jackson 2.12 through the canonical constructor, missing properties become null or defaults unless FAIL_ON_MISSING_CREATOR_PROPERTIES or FAIL_ON_NULL_CREATOR_PROPERTIES is on, and compact constructors are a good place for validation.
Open with
For the immutable class with final fields and no setters, Jackson has to call the constructor, so I tell it which one and how parameters map to fields: JsonCreator on the constructor and JsonProperty id and JsonProperty name on the parameters.
Then, if they want more
That works with any build setup. Alternatively, if the code is compiled with the -parameters flag and the ParameterNamesModule is registered, Jackson reads the names itself and I can drop the JsonProperty annotations; Spring Boot does both for us, which is why such classes often work in Boot without annotations, and Jackson 3 has the module built in.
One-argument constructors are still ambiguous, so there I set the creator mode explicitly. For a record I need nothing: since Jackson 2.12 it uses the canonical constructor, and records always keep their component names. Then I would talk about missing data.
If the JSON lacks name, the creator still gets called with null, or zero for a primitive, unless I enable FAIL_ON_MISSING_CREATOR_PROPERTIES or FAIL_ON_NULL_CREATOR_PROPERTIES. And I can put invariants in the record's compact constructor; the exception gets wrapped by Jackson and becomes a 400 in Spring MVC, while Bean Validation handles user-facing messages.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Deserialize {"id": 7, "name": "Ann"} into an immutable class with final fields and no...": https://scrivio-interview-library.netlify.app/spring-boot/json-serialization/a0edb10c49/