Code Beautifier
UnrecognizedPropertyException: Unrecognized field

Fix Jackson "UnrecognizedPropertyException"

Jackson refuses a JSON key that the Java class does not declare. Why FAIL_ON_UNKNOWN_PROPERTIES defaults to on, and the fixes: add, ignore, or configure.

Input that triggers it

{"id": 1, "name": "Ada", "department": "Analytical Engines"}
Open JSON to Java on its own page
Draft saved locally.

Local workspace

Named projects in IndexedDB · Local only — never synced to our servers. Worksp

Open manager

Batch workspace

Format multiple files locally in one run.

Style:
Annotations:
json
Javajava

What the error means

Jackson, the JSON library behind Spring and most of the Java ecosystem, is strict by default about extra keys. UnrecognizedPropertyException: Unrecognized field "department" (class com.example.User), not marked as ignorable means the JSON contained a property that the target class neither declares nor explicitly ignores. The exception lists the known property names, which is a quick way to see what the class does expect.

In the example, a User class generated from a sample without department will throw on this payload. Nothing is wrong with the JSON; the class and the data have drifted apart.

Why it happens

  • The class was generated from an incomplete sample, so optional keys that were absent from it are absent from the class.
  • The API evolved. The producer added a field; the consumer's model did not follow.
  • FAIL_ON_UNKNOWN_PROPERTIES is on, which it is by default in Jackson (though Spring Boot's auto-configured ObjectMapper turns it off, which is why the same class can work in one service and fail in another).
  • A naming mismatch: the JSON uses snake_case and the class uses camelCase without a naming strategy, so every key looks unknown.

How to fix it

There are three legitimate fixes, in order of preference:

  1. Add the field. Paste a full payload into the generator above; every key becomes a property, with @JsonProperty where the JSON name is not a valid Java identifier. This keeps the model honest.
  2. Ignore unknowns for this class with @JsonIgnoreProperties(ignoreUnknown = true) above the class declaration. The right choice for a consumer that should tolerate producer additions.
  3. Relax the mapper globally: mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false). Broadest and least safe; a typo in a key name is then silently dropped everywhere.

For a naming mismatch, set mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE) rather than annotating every field.

The class that accepts the example:

@JsonIgnoreProperties(ignoreUnknown = true)
public class User {
    public long id;
    public String name;
    public String department;
}

If it still fails

  • The exception message names the class. If it is not the one you expected, the payload's shape differs from the model's — an array where an object was expected, for instance, which is a different error (Expected BEGIN_OBJECT but was BEGIN_ARRAY).
  • JSON Diff between the generation sample and the failing payload lists exactly which keys were added.
  • The same policy question exists in Kotlin with kotlinx.serialization (ignoreUnknownKeys); JSON to Kotlin notes the setting.

Related errors