0% found this document useful (0 votes)
13 views

Java Not Able To Convert Json To Pojo Class

Uploaded by

Goxzalo hds
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Java Not Able To Convert Json To Pojo Class

Uploaded by

Goxzalo hds
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

13/10/21 23:28 java - Not able to convert json to pojo class, getting com.fasterxml.jackson.databind.exc.

UnrecognizedPropertyException exception - Stack Overflow

Not able to convert json to pojo class, getting


com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException
exception
This is my JsonObject

JSONObject input = new JSONObject("{\n" +


" \"ColumnNames\":[\"col1\", \"col2
"}");

My POJO Class

public class RequestClass {


private List<String> ColumnNames;

public void setColumnNames(List<String> ColumnNam


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_UNKNO

file:///D:/Downloads/Java-Not able to convert json to pojo class.html 1/3


13/10/21 23:28 java - Not able to convert json to pojo class, getting com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException exception - Stack Overflow

RequestClass request = null;


try {
request = mapper.readValue(input.toString(), Requ
} catch (Exception e) {
e.printStackTrace();
}

Getting an exception in output

com.fasterxml.jackson.databind.exc.UnrecognizedProper
at [Source: {"ColumnNames":["col1","col2","col3","co

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:

change the name of the property in the JSON to


columnNames , or

use an annotation to override the default introspective


behavior.

The latter is achieved by using the @JsonProperty on the


getter and setter, as follows:

@JsonProperty("ColumnNames")
public List<String> getColumnNames() {
return this.ColumnNames;
}

file:///D:/Downloads/Java-Not able to convert json to pojo class.html 2/3


13/10/21 23:28 java - Not able to convert json to pojo class, getting com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException exception - Stack Overflow

Looking at the exception it looks like in the pojo , you have


mentioned ColumnNames and in the json you have mentioned
columnNames (a case mismatch) , although you have defined it
correctly in the json example above. Please check whether there
is a case mismatch in the field names.

Thanks for the reply. But I checked with changing the name also in
pojo class, getting the same error. Doesn't seems like an issue with
case mismatch. – Rachit Jain Apr 2 '20 at 5:43

file:///D:/Downloads/Java-Not able to convert json to pojo class.html 3/3

You might also like