How To Solve Unrecognizedpropertyexception: Unrecognized Field, Non Marked Equally Ignorable - Json Parsing Mistake Using Jackson

While parsing JSON string received from i of our RESTful spider web services, I was getting this fault "Exception inward thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized champaign "person" (class Hello$Person), non marked equally ignorable". After exactly about research, I constitute that this is i of the mutual fault piece parsing JSON document using Jackson opened upwards source library inward Java application. The fault messages tell that it is non able to uncovering a suitable belongings advert called "person" inward our case, let's source accept a aspect at the JSON nosotros are trying to parse, the class nosotros are using to stand upwards for the JSON document together with the fault message itself.

Error Message:
Exception inward thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized champaign "person" (class Hello$Person), non marked equally ignorable (4 known properties: , "id", "city", "name", "phone"])

The fault messages tell that it tin uncovering out id, city, advert together with telephone attributes inward the Person class but non able to locate the "person" field.

Our POJO class looks similar below:

class Person{
   private int id;
   private String name;
   private String city;
   private long phone;

   .....

}


together with the JSON String:
{
  "person": [
   {
     "id": "11",
     "name": "John",
     "city": "NewYork",
     "phone": 7647388372
   }
  ]
}

If you lot aspect carefully, the "person" champaign points to a JSON array together with non object, which agency it cannot live mapped to mortal class directly.



How to solve this problem

Here are steps to solve this work together with acquire rid of this errorr:

1) Configure Jackson's ObjectMapper to non neglect when encounger unknown properties
You tin exercise this past times disabling DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES belongings of ObjectMapper equally shown below:

// Jackson code to convert JSON String to Java object
ObjectMapper objectMapper = novel ObjectMapper();
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Person p = objectMapper.readValue(JSON, Person.class);

System.out.println(p);

Now, the fault volition acquire away but the Output is non what you lot expected, it volition impress following:

Person [id=0, name=null, city=null, phone=0]

You tin regard that Person class is non created properly, the relevant attributes are null fifty-fifty though the JSON String contains its value.



The argue was that JSON String contains a JSON array, the mortal champaign is pointing towards an array together with at that spot is no champaign corresponding to that inward Person class.

In gild to properly parse the JSON String nosotros ask to exercise a wrapper class Community which volition have got an attribute to proceed an array of Person equally shown below:

static class Community {   individual List<Person> person;    populace List<Person> getPerson() {     return person;   }    populace void setPerson(List<Person> person) {     this.person = person;   }  }

Now, nosotros volition convert the JSON String to this Community class together with impress each mortal from the listing equally shown below:

ObjectMapper objectMapper = novel ObjectMapper();
//objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Community c = objectMapper.readValue(JSON, Community.class);

for (Person p : c.getPerson()) {
   System.out.println(p);
}

This volition impress the details of a mortal properly equally shown below:

Person [id=11, name=John, city=NewYork, phone=7647388372]

Now, coming dorsum to a to a greater extent than full general province of affairs where a novel champaign is added on JSON but non available inward your Person class, let's regard what happens.

Suppose, our JSON String to parse is following:

{
"person": [
{
"id": "11",
"name": "John",
"city": "NewYork",
"phone": 7647388372,
"facebook": "JohnTheGreat"
}
]
}

When you lot run the same computer program amongst this JSON String, you lot volition acquire next error:

While parsing JSON string received from i of our RESTful spider web services How to Solve UnrecognizedPropertyException: Unrecognized field, non marked equally ignorable - JSON Parsing Error using Jackson


Again, Jackson is non able to recognize the novel "facebook" property. Now, nosotros tin ignore this belongings past times disabling the characteristic which tells Jackson to neglect on the unknown belongings equally shown below:

ObjectMapper objectMapper = novel ObjectMapper();
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Community c = objectMapper.readValue(JSON, Community.class);

And this volition impress the mortal class properly equally shown below:

Person [id=11, name=John, city=NewYork, phone=7647388372]

Alternatively, you lot tin also role @JsonIgnoreProperties annotation to ignore undeclared properties.

The @JsonIgnoreProperties is a class-level annotation inward Jackson together with it volition ignore every belongings you lot haven't defined inward your POJO. Very useful when you lot are exactly looking for a couplet of properties inward the JSON together with don't desire to write the whole mapping.

This annotation provides command at class grade i.e. you lot tin tell Jackson that for this class, delight ignore whatever attribute non defined past times doing

@JsonIgnoreProperties(ignoreUnknown = true)

So, our Person class at nowadays looks like:

@JsonIgnoreProperties(ignoreUnknown = true)
static class Person{
private int id;
private String name;
private String city;
private long phone;

......

}


Sample program

import java.io.IOException; import java.util.List;  import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper;  /*  * {  "person": [  {  "id": "11",  "name": "John",  "city": "NewYork",  "phone": 7647388372  }  ]  }   */  public class Hello {    private static String JSON = "{\r\n" + " \"person\": [\r\n" + " {\r\n"       + " \"id\": \"11\",\r\n" + " \"name\": \"John\",\r\n"       + " \"city\": \"NewYork\",\r\n" + " \"phone\": 7647388372,\r\n"       + " \"facebook\": \"JohnTheGreat\"\r\n" + " }\r\n" + " ]\r\n" + " } ";    public static void main(String args[]) throws JsonParseException,       JsonMappingException, IOException {      ObjectMapper objectMapper = new ObjectMapper();     objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);     Community c = objectMapper.readValue(JSON, Community.class);      for (Person p : c.getPerson()) {       System.out.println(p);     }    }    static class Community {     private List<Person> person;      public List<Person> getPerson() {       return person;     }      public void setPerson(List<Person> person) {       this.person = person;     }    }    static class Person {     private int id;     private String name;     private String city;     private long phone;      public int getId() {       return id;     }      public void setId(int id) {       this.id = id;     }      public String getName() {       return name;     }      public void setName(String name) {       this.name = name;     }      public String getCity() {       return city;     }      public void setCity(String city) {       this.city = city;     }      public long getPhone() {       return phone;     }      public void setPhone(long phone) {       this.phone = phone;     }      @Override     public String toString() {       return "Person [id=" + id + ", name=" + advert + ", city=" + metropolis           + ", phone=" + telephone + "]";     }    } } 

When I run source version of this program, I was greeted amongst the next error:

Exception inward thread "main" com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor constitute for type [simple type, class Hello$Person]: tin non instantiate from JSON object (need to add/enable type information?)
at [Source: java.io.StringReader@5e329ba8; line: 2, column: 3]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:984)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:276)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
at Hello.main(Hello.java:40)


This fault was occurring because my nested class Person was non static, which agency it cannot live instantiated because having whatever Outer class instance. The resultant resolved afterwards making the Person class static.

If you lot are non familiar amongst this item before, I advise you lot cheque Java Fundamentals: The Core Platform, a gratis class from Pluralsight to acquire to a greater extent than most such details of Java programming language. You tin signup for a gratis trial, which gives you lot 10 days access, plenty to acquire whole Java for free.

While parsing JSON string received from i of our RESTful spider web services How to Solve UnrecognizedPropertyException: Unrecognized field, non marked equally ignorable - JSON Parsing Error using Jackson



Now, let's regard the existent error:

Exception inward thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized champaign "person" (class Hello$Person), non marked equally ignorable (4 known properties: , "id", "city", "name", "phone"])
at [Source: java.io.StringReader@4fbc9499; line: 2, column: 14] (through reference chain: Person["person"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:79)
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:555)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:708)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1160)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:315)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
at Hello.main(Hello.java:40)

When you lot run the lastly version of the computer program you lot volition regard next output:

Person [id=11, name=John, city=NewYork, phone=7647388372]

This agency nosotros are able to parse JSON containing unknown attributes successfully inward Jackson.




How to compile together with run this program?

You tin merely re-create glue the code into your favorite IDE e.g. Eclipse to compile together with run the program.

In Eclipse, you lot don't fifty-fifty ask to exercise the class file because it volition automatically exercise the class together with bundle if you lot re-create glue the code inward Java project.

If Eclipse is your primary IDE together with you lot desire to acquire to a greater extent than of such productivity tips I advise you lot cheque out The Eclipse Guided Tour - Part 1 together with ii By Tod Gentille.

While parsing JSON string received from i of our RESTful spider web services How to Solve UnrecognizedPropertyException: Unrecognized field, non marked equally ignorable - JSON Parsing Error using Jackson


It's a free, online class to acquire both basic together with advanced characteristic of Eclipse IDE, which every Java developer should live aware of. You tin acquire access to this class past times signing upwards for a gratis trial, which gives you lot 10 days access to the whole Pluralsight library, i of the most valuable collection to acquire most programming together with other technology. Btw, 10 days is to a greater extent than than plenty to acquire Java together with Eclipse together.

Anyway, i time you lot re-create glue the code, all you lot ask to exercise is either include Maven dependency inward your pom.xml or manually download required JAR file for Jackson opened upwards source library.

For Maven Users
You tin add together next Maven dependency on your project's pom.xml together with and then run the mvn build or mvn install command to compile:


<dependency>   <groupId>com.fasterxml.jackson.core</groupId>   <artifactId>jackson-databind</artifactId>   <version>2.2.3</version> </dependency>

This dependency requires jackson-core together with jackson-annotations but Maven volition automatically download that for you.

Manually Downloading JAR
If you lot are non using Maven or whatever other build tool e.g.gradle together with then you lot tin exactly acquire to Maven primal library together with download next iii JAR files together with include them inward your classpath:

jackson-databind-2.2.3.jar
jackson-core-2.2.3.jar
jackson-annotations-2.2.3.jar

Once you lot compiled the class successfully you lot tin run them equally you lot run whatever other Java computer program inward Eclipse, equally shown hither or you lot tin run the JAR file using the command work equally shown here.

In short, The "com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized champaign XXX, non marked equally ignorable" fault comes when you lot attempt to parse JSON to a Java object which doesn't incorporate all the fields defined inward JSON. You tin solve this fault past times either disabling the characteristic of Jackson which tells it to neglect if run across unknown properties or past times using annotation @JsonIgnoreProperties at the class level.

Further Learning
REST amongst Spring past times Eugen Paraschiv
REST API Design, Development & Management
Java Web Fundamentals

Thanks for reading this article thence far. If you lot similar my explanation together with then delight percentage amongst your friends together with colleagues. If you lot have got whatever questions or feedback, delight drib a  note. 

Belum ada Komentar untuk "How To Solve Unrecognizedpropertyexception: Unrecognized Field, Non Marked Equally Ignorable - Json Parsing Mistake Using Jackson"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel