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_PROPERTIESis on, which it is by default in Jackson (though Spring Boot's auto-configuredObjectMapperturns it off, which is why the same class can work in one service and fail in another).- A naming mismatch: the JSON uses
snake_caseand the class usescamelCasewithout a naming strategy, so every key looks unknown.
How to fix it
There are three legitimate fixes, in order of preference:
- Add the field. Paste a full payload into the generator above; every key becomes a property, with
@JsonPropertywhere the JSON name is not a valid Java identifier. This keeps the model honest. - Ignore unknowns for this class with
@JsonIgnoreProperties(ignoreUnknown = true)above the class declaration. The right choice for a consumer that should tolerate producer additions. - 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
Cannot map `null` into type `long`
Declare the field as the boxed type (Long, Integer, Boolean) so it can be null, or make the producer omit the key instead of sending null.
json: unknown field
Add the missing field to the struct (regenerate from a fuller sample), or stop calling DisallowUnknownFields if extra keys are acceptable; the default decoder ignores them.