This is my JsonObject
JSONObject input = new JSONObject("{\n" +
" \"ColumnNames\":[\"col1\", \"col2\", \"col3\", \"col4\", \"col5\"]\n" +
"}");
My POJO Class
public class RequestClass {
private List<String> ColumnNames;
public void setColumnNames(List<String> ColumnNames) {
this.ColumnNames = ColumnNames;
}
public List<String> getColumnNames() {
return this.ColumnNames;
}
}
Trying to convert JsonObject to pojo class object with the help of ObjectMapper as shown below -
ObjectMapper mapper = new ObjectMapper();
//mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
RequestClass request = null;
try {
request = mapper.readValue(input.toString(), RequestClass.class);
} catch (Exception e) {
e.printStackTrace();
}
Getting an exception in output
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "ColumnNames" (class RequestClass), not marked as ignorable (one known property: "columnNames"])
at [Source: {"ColumnNames":["col1","col2","col3","col4","col5"]}; line: 1, column: 17] (through reference chain: RequestClass["ColumnNames"])
The name of the private property named ColumnNames
is actually irrelevant. The property is found by introspection, looking at the getters and setters. And by convention, if you have methods named getColumnNames
and setColumnNames
, they define a property named columnNames
(lowercase c
).
So you have two choices:
columnNames
, orThe latter is achieved by using the @JsonProperty on the getter and setter, as follows:
@JsonProperty("ColumnNames")
public List<String> getColumnNames() {
return this.ColumnNames;
}