java - Jackson unrecognized field for class name -
following how json string looks
{ "employee": { "id": "c1654935-2602-4a0d-ad0f-ca1d514a8a5d", "name": "smith" ... } } now using objectmapper#readvalue(jsonasstr,employee.class) convert json. employee class follows...
@xmlrootelement(name="employee") public class employee implements serializable { private string id; private string name; ... public employee() { } @xmlelement(name="id") public string getid() { return id; } public void setid(string id) { this.id= id; } @xmlelement(name="name") public string getname() { return name; } public void setname(string name) { this.name = name; } ... } the exception getting
com.fasterxml.jackson.databind.exc.unrecognizedpropertyexception: unrecognized field "employee" (class com.abc.employee), not marked ignorable (12 known properties: , "id", "name", ... [truncated]]) i not able understand why "employee" considered property. wrong in assuming class members considered properties?
the problem json object { } maps java class, , properties in json map java properties. first { } in json (which trying unmarshal employee), has property employee, employee class not have property for. that's why getting error. if try , unmarshal enclosed { }
{ "id": "c1654935-2602-4a0d-ad0f-ca1d514a8a5d", "name": "smith" } it work employee has properties. if don't have control on json, can configure objectmapper unwrap root value
objectmapper mapper = new objectmapper(); mapper.configure(deserializationfeature.unwrap_root_value, true); but might have problem. unwrapping based on annotation on employee class, either @jsonrootname("employee") or @xmlrootelement(name = "employee"). latter though, need make sure have jaxb annotation support. that, need have jackson-module-jaxb-annotations, register module
mapper.registermodule(new jaxbannotationmodule()); this applies jaxb annotations you're using. without module, won't work.
Comments
Post a Comment