You may have found yourself in this situation more than once: you're trying to convert a JSON object to a Java object and Jackson gives you this:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field

Well that's a pain.

But it's a fixable pain.

And in this guide, I'll show you a couple of ways to fix it.

The Dilemma

Let's say, for example, you've got an Employee class that looks like this:

public class Employee {

    private String id;    
    private String lastName;
    private String firstName;    
...
}

As you can see, it's only got three fields: idlastName, and firstName.

Now let's say you try to deserialize the following JSON object into an instance of that class:

{
  "id" : "A17",
  "lastName" : "Smith",
  "firstName" : "Frank",
  "shirtSize" : "XXL"
}

If you look closely, you'll see that the JSON object has a field that's not defined in the Employee class: "shirtSize".

The code you use to deserialize looks like this:

ObjectMapper mapper = new ObjectMapper();  
Employee employee = mapper.readValue(json, Employee.class);

When you run that code, you'll end up with that exception you saw at the beginning of the article.

More specifically, Jackson will give you this output:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "shirtSize" (class Employee), not marked as ignorable (3 known properties: "lastName", "id", "firstName"])
 at [Source: (String)"{
  "id" : "A17",
  "lastName" : "Smith",
  "firstName" : "Frank",
  "shirtSize" : "XXL"
}"; line: 5, column: 18] (through reference chain: Employee["shirtSize"])

That's a pretty thorough explanation of the problem. My hat's off to the Jackson developer who insisted on dispensing that much detail.

And that "not marked as ignorable" bit is a big clue about how you can solve that problem.

Going Ignorant

Perhaps the easiest way to fix the problem is with an annotation in the plain old Java object (POJO). You can do it like this:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Employee {

    private String id;    
    private String lastName;
    private String firstName;
...
}

That will tell Jackson to ignore every property not defined in the POJO.

Again: that's useful if the service you're calling gives you a huge object and you just want a few properties.

And if you run that deserialization code again, you'll see that everything works just fine. No exceptions.

Another Way to Get Ignorant

There's another option to handling this problem if you don't want to annotate the Java class. You can configure the ObjectMapper object.

Do this:

ObjectMapper mapper = new ObjectMapper();  
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Employee employee = mapper.readValue(json, Employee.class);

That will also solve your problem.

I'd explain what

mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

means but I think the verbiage is fairly self-explanatory. Once again: my hat's off to the Jackson development team.

By the way, you can use the DeserializationFeature enum for other configuration settings as well. I definitely recommend checking it out.

Wrapping It Up

There you go. A couple of easy ways to get around that pesky UnrecognizedPropertyException.

Now it's over to you. Take what you've learned here and make it your own.

Have fun!

Photo by Tim Douglas from Pexels