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.
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.
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; ... }
If the JSON string consists of properties that cannot be mapped to java class attributes, then we encounter UnrecognizedPropertyException.
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 { ... }
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));
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With