1. Overview
In this article, we will learn to fix the Jackson UnrecognizedPropertyException: Unrecognized field, not marked as ignorable.
To learn more about the Jackson library, refer to our articles.
2. Jackson Unrecognized field, not marked as ignorable
Jackson ObjectMapper class supports both serialization and deserialization. Deserialization refers to converting JSON back to a Java object.
When your JSON contains additional fields than your Java object, then ObjectMapper throws the UnrecognizedPropertyException: Unrecognized field, not marked as ignorable during deserialization.
For example, the below Student
class contains only the id
field.
public class Student { public Student(long id) { this.id = id; } public Student() { } public long id; @Override public String toString() { return "Student [id=" + id + "]"; } }
Let’s assume that we had to deserialize the below JSON that contains id
and name
fields to the Student
java object.
@Test void unknownProperties_exception() throws JsonMappingException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); String json = "{\"id\": 1001, \"name\": \"Jackson\"}"; Student student = mapper.readValue(json, Student.class); System.out.println(student); }
Since the name
field doesn’t exist in the Student
class, the above deserialization results in the UnrecognizedPropertyException error.
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "name" (class com.tedblob.jackson.examples.models.unknownproperties.Student), not marked as ignorable (one known property: "id"])
3. Solutions to fix Unrecognized field, not marked as ignorable
The possible reasons for this exception are:
- You don’t need all JSON fields in your Java class. So you skipped defining the unnecessary fields in the Java class. Check the next section for solutions.
- You declared the fields in Java class but they are private and doesn’t have any setter or getter methods defined for them. Be default, Jackson can deserialize only the fields that are public or contains setter or getters. To deserialize the private fields, see this article.
3.2. @JsonIgnoreProperties at the class level
You can use @JsonIgnoreProperties to ignore unknown properties at the class level. The ignoreUnknown
property defines whether to ignore the unrecognized properties during the deserialization. If we specified it as true
, then Jackson ignores unrecognized properties during deserialization.
@JsonIgnoreProperties(ignoreUnknown = true) public class Student { public Student(long id) { this.id = id; } public Student() { } public long id; @Override public String toString() { return "Student [id=" + id + "]"; } }
3.3. Configure ObjectMapper globally to fix Unrecognized field, not marked as ignorable
You can ignore the unrecognized fields by configuring the ObjectMapper
class:
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
By declaring the FAIL_ON_UNKNOWN_PROPERTIES
as false, the unrecognized properties are ignored.
@Test void unknownProperties_configureMapper() throws JsonMappingException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); String json = "{\"id\": 1001, \"name\": \"Jackson\"}"; Student student = mapper.readValue(json, Student.class); System.out.println(student); }
If you run the above test, then the JSON is deserialized properly to Student
object.
Student [id=1001]
3.4. Ignore unrecognized properties globally using Jackson2ObjectMapperBuilder
If your project contains spring-web dependency, then you can use Jackson2ObjectMapperBuilder
to build your ObjectMapper
instance. By default, Jackson2ObjectMapperBuilder
ignores the unrecognized properties and doesn’t throw any exception.
@Autowired Jackson2ObjectMapperBuilder jsonBuilder; @Test void unknownProperties_JacksonBuilder() throws JsonMappingException, JsonProcessingException { ObjectMapper mapper = jsonBuilder.build(); String json = "{\"id\": 1001, \"name\": \"Jackson\"}"; Student student = mapper.readValue(json, Student.class); System.out.println(student); }
4. Conclusion
To sum up, we have learned the different solutions to fix the Jackson Unrecognized field, not marked as ignorable.
Τhere’s ⅽertainly a lot to learn about this topic.
I love all of the points you’ve made.
Pingback: Unrecognized Field Not Marked As Ignorable? 20 Most Correct Answers - Chambazone.com