java - Jackson Json to POJO mapping -
i little lost in creating mapping jackson. json has following structure
{ "d": { "__metadata": { "uri": "https://apisalesdemo8.successfactors.com:443/odata/v2/jobapplication(1463l)", "type": "sfodata.jobapplication" }, "lastname": "k", "address": "123 main street", "cellphone": "12345", "firstname": "katrin", "city": "anytown", "country": "united states", "custappattachment": { "results": [ { "__metadata": { "uri": "https://apisalesdemo8.successfactors.com:443/odata/v2/attachment(1188l)", "type": "sfodata.attachment" }, "fileextension": "jpeg", "filename": "hp-hero-img.jpeg", "filecontent": "/9j/4aa" }, { "__metadata": { "uri": "https://apisalesdemo8.successfactors.com:443/odata/v2/attachment(1189l)", "type": "sfodata.attachment" }, "fileextension": "jpeg", "filename": "hp-content-bkgd-img.jpeg", "filecontent": "/9j/4aaqsk" }]}}}
i find lot of tutorials handling arrays, fail first token "d". , "__metadata" token not needed @ all.
i created pojo containing attributes lastname etc. , collection attachments. code fails @ token "d" or "__metadata"
public class responsedataobject { private string lastname; private string address; private string cellphone; private string firstname; private string city; private string country; private list<attachment> attachments = new arraylist<>(); .....getters , setters
and jackson reader
objectreader objectreader = mapper.readerfor(responsedataobject.class); responsedataobject dataobject = objectreader.readvalue(file);
any hints appreciated.
regards mathias
you need ignore properties, not present in pojo. set following property in deserializationfeature
objectmapper
:
// version 1.x mapper.configure(deserializationconfig.feature.fail_on_unknown_properties, false); // newer versions mapper.configure(deserializationfeature.fail_on_unknown_properties, false)
deserialization code:
objectmapper mapper = new objectmapper(); mapper.configure(deserializationfeature.fail_on_unknown_properties, false); mapper.configure(deserializationfeature.unwrap_root_value, true); responsedataobject dataobject = mapper.readvalue(file, responsedataobject.class);
and add annotation responsedataobject class:
@jsonrootname(value = "d") class responsedataobject {
Comments
Post a Comment