com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field
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