Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

json

jackson

i got a deserialization problem:

This is my class:

public class Response {

    private Object ResObj;
    private int ResInt;

    public Object getResObj() {
        return ResObj;
    }

    public int getResInt() {
        return ResInt;
    } 
} 

the JSON i want to deserialize is:

{"ResObj":{"ClientNum":"12345","ServerNum":"78945","IdNum":"020252"},"ResInt":0}

I get this exception:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "ResObj" , not marked as ignorable (0 known properties: ])
 at [Source: java.io.StringReader@1f758500; line: 1, column: 20] (through reference chain: ["ResObj"])

I don't want to add:

@JsonIgnoreProperties(ignoreUnknown = true)

because I want to get the ResObj...

if I add the annotation, it pass but it will set it as null .. which I don't want.

like image 268
user2212726 Avatar asked May 05 '14 10:05

user2212726


People also ask

How do I ignore unrecognized fields Jackson?

Alternatively, you can also use @JsonIgnoreProperties annotation to ignore undeclared properties. The @JsonIgnoreProperties is a class-level annotation in Jackson and it will ignore every property you haven't defined in your POJO.

How do I fix UnrecognizedPropertyException?

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; ... }

What is UnrecognizedPropertyException in Java?

If the JSON string consists of properties that cannot be mapped to java class attributes, then we encounter UnrecognizedPropertyException.

How do I ignore properties in Jackson?

Ignore Fields Using Filters Finally, we can also use filters to ignore specific fields in Jackson. First, we need to define the filter on the Java object: @JsonFilter("myFilter") public class MyDtoWithFilter { ... }


3 Answers

If you don't want to have a setter in your bean and only use fields and getters, you can use the visibility checker of ObjectMapper to allow field visibility.
Something like following:

ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.setVisibility(VisibilityChecker.Std.defaultInstance().withFieldVisibility(JsonAutoDetect.Visibility.ANY));
like image 112
Jay Avatar answered Oct 06 '22 09:10

Jay


You need Setter methods to allow Jackson to set the properties, and you need to change the fields in the json to begin with a lower case letter:

public class Response {

    private Object ResObj;
    private int ResInt;

    public Object getResObj() {
        return ResObj;
    }

    public void setResObj(Object ResObj) {
        this.ResObj = ResObj;
    }

    // ...
}

and:

{"resObj":{"clientNum":"12345","serverNum":"78945","idNum":"020252"},"resInt":0}

The reason for the JSON change is that the Jackson bean serialisation will reflect over the class, and when it sees getXyz() and setXyz() methods will map these to a Json filed names "xyz" (and not "Xyz").

I think there are several ways to override this behaviour, one is to use the one of the Jackson annotations.

like image 33
jon-hanson Avatar answered Oct 06 '22 08:10

jon-hanson


I think you should try this

public class Response {
    @JsonProperty
    private Object ResObj;
    @JsonProperty
    private int ResInt;

    public Object getResObj() {
        return ResObj;
    }

    public int getResInt() {
        return ResInt;
    } 
} 

It will resolve your issue with UnrecognizedPropertyExceptions

like image 6
Ashish Avatar answered Oct 06 '22 10:10

Ashish