Unrecognized field "X" (class Y), not marked as ignorable
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "carrier" (class com.example.Order), not marked as ignorable (2 known properties: "status", "id")
What it means
The JSON has a key, carrier, that your class does not have, and a plain Jackson 2 ObjectMapper fails on unknown keys. Add @JsonIgnoreProperties(ignoreUnknown = true) to the class, or add the field if you need it.
Last verified on Jackson 2.21 and 3.1; Spring 6.2 and 7.0.
This JSON is valid. The problem is on the Java side, so a JSON validator would say it is fine.
- Usually
- The sender added a fieldA misspelled or renamed keyIt works in Spring but fails in your test
- Fix
- Add
@JsonIgnoreProperties(ignoreUnknown = true)to the class, as in the fixed code.
The fix
Add @JsonIgnoreProperties(ignoreUnknown = true) to the class, as in the fixed code. If the key was renamed, add or rename the field instead.
{
"id": 7,
"status": "shipped",
"carrier": "DHL"
}public class Order {
public long id;
public String status;
}
Order order = mapper.readValue(json, Order.class);Line 1 changes.
@JsonIgnoreProperties(ignoreUnknown = true)
public class Order {
public long id;
public String status;
}
Order order = mapper.readValue(json, Order.class);How the message appears
Only a plain Jackson 2 ObjectMapper throws this, so there is one error and where it stopped. Jackson 3 and Spring 6.2 and 7.0 read the same JSON and skip the unknown key.
- com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "carrier" (class com.example.Order), not marked as ignorable (2 known properties: "status", "id")
- at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 4, column: 15] (through reference chain: com.example.Order["carrier"])
What causes it
The sender added a field
The other side added a field and your class has not caught up. The message tells you both sides: the key it did not know,
"carrier", and the ones it does,(2 known properties: "status", "id").A misspelled or renamed key
If the JSON says
carrierand your field isshipper, Jackson cannot match them. Compare the unknown key with the known properties in the message.It works in Spring but fails in your test
Jackson 3 and Spring 6.2 and 7.0 read this same JSON and skipped
carrier. Only a plain Jackson 2ObjectMapperthrew. So a test that creates its ownnew ObjectMapper()fails while the app works. See the Spring page.
Worth knowing
Watch outIgnoring unknown keys also hides a misspelled one. The field just stays empty and nothing tells you. Use it where the sender is allowed to add fields, not everywhere.