Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson with JSON: Unrecognized field, not marked as ignorable

I need to convert a certain JSON string to a Java object. I am using Jackson for JSON handling. I have no control over the input JSON (I read from a web service). This is my input JSON:

{"wrapper":[{"id":"13","name":"Fred"}]} 

Here is a simplified use case:

private void tryReading() {     String jsonStr = "{\"wrapper\"\:[{\"id\":\"13\",\"name\":\"Fred\"}]}";     ObjectMapper mapper = new ObjectMapper();       Wrapper wrapper = null;     try {         wrapper = mapper.readValue(jsonStr , Wrapper.class);     } catch (Exception e) {         e.printStackTrace();     }     System.out.println("wrapper = " + wrapper); } 

My entity class is:

public Class Student {      private String name;     private String id;     //getters & setters for name & id here } 

My Wrapper class is basically a container object to get my list of students:

public Class Wrapper {     private List<Student> students;     //getters & setters here } 

I keep getting this error and "wrapper" returns null. I am not sure what's missing. Can someone help please?

org.codehaus.jackson.map.exc.UnrecognizedPropertyException:      Unrecognized field "wrapper" (Class Wrapper), not marked as ignorable  at [Source: java.io.StringReader@1198891; line: 1, column: 13]      (through reference chain: Wrapper["wrapper"])  at org.codehaus.jackson.map.exc.UnrecognizedPropertyException     .from(UnrecognizedPropertyException.java:53) 
like image 941
jshree Avatar asked Dec 20 '10 04:12

jshree


People also ask

How do I resolve Unrecognizedpropertyexception?

In order to solve this issue, we disable the object mapper to deserialize on unknown properties. So the object mapper will unmarshal/deserialize only those properties from json file which are mapped to the java class. We have emp1. json which is similar to the previous example.

How do you ignore fields in Jackson?

Ignore All Fields by Type Finally, we can ignore all fields of a specified type, using the @JsonIgnoreType annotation. If we control the type, then we can annotate the class directly: @JsonIgnoreType public class SomeType { ... } More often than not, however, we don't have control of the class itself.

How do I ignore JSON fields?

If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.

What is Fail_on_unknown_properties?

FAIL_ON_UNKNOWN_PROPERTIES. Feature that determines whether encountering of unknown properties (ones that do not map to a property, and there is no "any setter" or handler that can handle it) should result in a failure (by throwing a JsonMappingException ) or not.


1 Answers

You can use Jackson's class-level annotation:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties  @JsonIgnoreProperties class { ... } 

It will ignore every property you haven't defined in your POJO. Very useful when you are just looking for a couple of properties in the JSON and don't want to write the whole mapping. More info at Jackson's website. If you want to ignore any non declared property, you should write:

@JsonIgnoreProperties(ignoreUnknown = true) 
like image 154
Ariel Kogan Avatar answered Oct 05 '22 02:10

Ariel Kogan