Quick revision
The short answer
Plain Jackson fails with UnrecognizedPropertyException, because DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES is enabled by default. Spring Boot's ObjectMapper disables that feature, so in a Boot application unknown fields are silently ignored. For one class we can choose with @JsonIgnoreProperties(ignoreUnknown = true), and with @JsonAnySetter we can collect unknown fields into a map instead of dropping them.
What to remember
In this answer, we saw that plain Jackson fails with UnrecognizedPropertyException on unknown fields because FAIL_ON_UNKNOWN_PROPERTIES is enabled by default, while Spring Boot's mapper disables it so unknown fields are ignored, which is also why tests with new ObjectMapper() can disagree with the application. @JsonIgnoreProperties(ignoreUnknown = true) makes one class tolerant with any mapper, @JsonAnySetter collects unknown fields into a map and @JsonAnyGetter writes them back, and we balance tolerance with validation, logging and strict reading where silent mistakes are costly.
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.
Plain Jackson fails with UnrecognizedPropertyException, because DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES is enabled by default. Spring Boot's ObjectMapper disables that feature, so in a Boot application unknown fields are silently ignored. For one class we can choose with @JsonIgnoreProperties(ignoreUnknown = true), and with @JsonAnySetter we can collect unknown fields into a map instead of dropping them.
Why this question is asked
A client sends a field our class does not have: an old app sends a field we removed, a new app sends one we have not added yet, or someone simply misspells quantity. What happens next depends on which mapper reads the request, and the difference between plain Jackson and Spring Boot surprises people in both directions: a unit test with new ObjectMapper() fails while the running application accepts the same JSON. Interviewers ask this because it tests knowledge of defaults and the judgement of when strictness helps. In this answer, we will see the default in plain Jackson, the opposite default in Spring Boot, how to control it per class, how to keep the unknown fields, and how to decide which behaviour an API should have.
1Plain Jackson: Unknown Fields Fail
When Jackson reads an object, it matches each JSON field to a property of the target class. With its own defaults, a field that matches nothing is an error, because FAIL_ON_UNKNOWN_PROPERTIES is enabled. The exception names the field, the class and the known properties, which makes typos easy to spot.
Notice that Jackson read name and email without trouble; it stopped at the first unknown field. The design reason for this default is safety: in plain Jackson, an unexpected field usually means the JSON and the class disagree, and failing loudly is better than producing an object that silently lacks data.
2Spring Boot: Unknown Fields Are Ignored
Spring's Jackson2ObjectMapperBuilder, which Spring Boot uses to build its mapper, disables FAIL_ON_UNKNOWN_PROPERTIES. So in a Boot application, the same request reaches the controller with name and email set and nickname dropped, and the client gets a normal response. The reasoning is that a web API should be tolerant: clients and servers are released separately, and a client that sends a field the server does not know yet, or no longer uses, should not get an error for it.
| Setup | Result |
|---|---|
new ObjectMapper() (Jackson 2 defaults) | UnrecognizedPropertyException |
| Spring Boot's mapper | ignored, request succeeds |
| Boot + fail-on-unknown-properties=true | 400 Bad Request |
@JsonIgnoreProperties(ignoreUnknown = true) on the class | ignored with any mapper |
@JsonAnySetter on the class | kept in a map |
Notice why the test and the application disagree: the test used new ObjectMapper() with Jackson's defaults, while the application uses Boot's mapper. Testing with the injected mapper, or a @JsonTest slice that builds it the Boot way, makes the test match production. In Jackson 3, which Spring Boot 4 uses, check the release notes for this feature's default; Boot sets its own value either way.
3Choosing per Class With @JsonIgnoreProperties
When a class should ignore unknown fields regardless of the mapper, we say so on the class. This is common for classes that read third-party data, such as a webhook payload from a payment provider that adds fields regularly, and for event classes in shared libraries, which may be read by mappers configured in different ways.
Notice that the annotation only adds tolerance. Setting ignoreUnknown = false on a class does not make it strict when the mapper ignores unknown fields globally; it simply means the annotation does not ignore them, and the mapper's setting decides. For strictness on one endpoint in a lenient application, we can read the body with an ObjectReader that enables the feature, or validate the raw JSON against a schema.
4Keeping Unknown Fields With @JsonAnySetter
Sometimes ignoring is too much and failing is too little. A proxy or an integration may need to pass on fields it does not understand, or we want to log which unknown fields clients send before deciding to support them. @JsonAnySetter marks a method or a map field that receives every property Jackson could not match, and @JsonAnyGetter writes them back out.
Notice that the unknown fields survive a round trip, which is what a pass-through service needs. With any-setter present, unknown fields never cause an error, whatever the mapper's setting. Recent Jackson versions also support @JsonAnySetter on a record component or creator parameter; check your version before relying on it with records.
5Deciding Which Behaviour an API Should Have
Ignoring unknown fields is the right default for most APIs and almost all events, because it lets each side evolve on its own: a producer can add a field and old consumers keep working. Its cost is silent mistakes. A client that sends quantitty instead of quantity gets a normal response and an order for the default quantity, and nobody notices until a customer complains.
So we combine tolerance with checks where mistakes are expensive. Required fields get @NotNull or @NotBlank validation, so a misspelled required field fails with a clear 400 even though the unknown name was ignored. For high-risk requests, such as payment instructions or admin changes, a strict reader for that endpoint is reasonable. And logging the names of unknown fields, through an any-setter or a DeserializationProblemHandler, shows us when clients send something we do not understand, without failing their requests.
6Follow-up questions
Why does my unit test fail when the application accepts the JSON?
The test most likely creates new ObjectMapper(), which uses Jackson's own default and fails on unknown properties, while the application uses Spring Boot's mapper with the feature disabled. Inject the application's mapper in the test, or use @JsonTest, so both use the same configuration.
Does ignoring unknown properties hide misspelled fields?
Yes, that is its main cost. Validation on required fields catches the important cases, because the correctly spelled field arrives empty and fails @NotNull. For optional fields, a typo stays silent, which is why some teams log unknown field names or enable strict reading on sensitive endpoints.
What happens with unknown enum values?
That is a separate feature. An unknown value such as "tier":"platinum" for an enum without that constant fails by default even in Spring Boot. READ_UNKNOWN_ENUM_VALUES_AS_NULL or READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE with @JsonEnumDefaultValue make it tolerant, which matters for evolving events.
7Conclusion
In this answer, we saw that plain Jackson fails with UnrecognizedPropertyException on unknown fields because FAIL_ON_UNKNOWN_PROPERTIES is enabled by default, while Spring Boot's mapper disables it so unknown fields are ignored, which is also why tests with new ObjectMapper() can disagree with the application. @JsonIgnoreProperties(ignoreUnknown = true) makes one class tolerant with any mapper, @JsonAnySetter collects unknown fields into a map and @JsonAnyGetter writes them back, and we balance tolerance with validation, logging and strict reading where silent mistakes are costly.
Open with
In plain Jackson, an unknown field makes deserialization fail with UnrecognizedPropertyException, because DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES is enabled by default. The message names the field and lists the known properties. Spring Boot is different: it builds its ObjectMapper with Spring's Jackson2ObjectMapperBuilder, which disables that feature, so in a Boot application the unknown field is simply ignored and the request succeeds.
Then, if they want more
That's why a unit test with new ObjectMapper() can fail on JSON the running app accepts; I'd test with the injected mapper or @JsonTest. If I want the whole app strict, I set spring.jackson.deserialization.fail-on-unknown-properties=true and unknown fields become a 400.
For a single class, @JsonIgnoreProperties(ignoreUnknown = true) makes it tolerant with any mapper, which I use for third-party webhooks and shared event classes. If I want to keep the unknown fields, for a pass-through service or to log what clients send, I use @JsonAnySetter to collect them into a map, and @JsonAnyGetter to write them back out.
On the design side, ignoring unknown fields is the right default for evolving APIs and events, because producers can add fields without breaking older consumers. The cost is that typos are silent, so I rely on validation of required fields, log unknown field names, and use strict reading for sensitive endpoints like payments.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 4
- 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 "What happens when a JSON request contains a field that the target class does not have...": https://scrivio-interview-library.netlify.app/spring-boot/json-serialization/38d29805d1/