java - Why Json ask for no argument constructor for Junit test? -
i have problem deserializing json string using jackson. getting error : com.fasterxml.jackson.databind.jsonmappingexception: no suitable constructor found type [simple type, class ratingdmo]: can not instantiate json object (need add/enable type information?)
i have fields mapped json file , parameters available properties , have same type.
note :- if provide no-arg constructor(just checking if works), test works.
i can't provide/leave default constructor in code have few final fields in model.
mapper.readvalue(new file("ratingrequest.json"), ratingdmo.class);
json file :-
{ "company": "test", "state": "nj", "lob": "pa", "channel": "ia", "username": "globalbatchuser", "password": "globalbatchuser", "policynumber": "test", "policykey": 7267, "transactiontype": "quote", "evaluationtype": null, "clientid": null, "policyterm": "6", "effectivedate": "01/01/1980", "policyxml": null, "firstname": null, "lastname": null, "businessname": null, "city": null, "zip": null, "effectivedtstart": "06/06/2013", "effectivedtend": "06/06/2014", "searchtype": "uw1", "uuid": null, "policystatuscodes": [], "eadvisorcoverages": {}, "customcoverages": {}, "defaultcoverages": {}, "action": null, "type": null, "selectedcoverage": null, "motorclubmember": true, "selectedsubcoveres": null, "customrated": true, "policydmo": { "policykey": 0, "company": "hp", "state": "nj", "lob": "auto", "channel": null, "addresses": [ { "addressid": 0, "addrline1txt": "20 martin rd", "addrline2txt": null, "addrline3txt": null, "addrline4txt": null, "cityname": "livingston", "cityaliasname": null, "countyname": null, "countrycd": null, "districtcd": null, "housenum": null, "poboxind": null, "statecd": "nj", "zip": "07039", "zipplusfour": null, "addresstype": "address_residence_key", "vehicleid": 0 }, { "addressid": 0, "addrline1txt": "20 martin rd", "addrline2txt": null, "addrline3txt": null, "addrline4txt": null, "cityname": "livingston", "cityaliasname": null, "countyname": null, "countrycd": null, "districtcd": null, "housenum": null, "poboxind": null, "statecd": "nj", "zip": "07039", "zipplusfour": null, "addresstype": "address_mailing_key", "vehicleid": 0 } ], "policyinfo": { "policykey": 0, "mailingaddrid": 0, "prioraddrid": 0, "residentialaddrid": 0, "billingaddrid": 0, "employeraddrid": 0, "policynum": "test", "policyeffdt": 1371614400000, "policyexpdt": null, "companycd": null, "statecd": "nj", "term": 0, "emailid": "ksiripurapu@plymouthrock.com", "faxnum": null, "homephonenum": null, "workphonenum": null, "mailingaddrissameind": "y", "plfcd": "hipt", "affinitycd": null, "motorclubind": null, "homeownerind": null, "homeinscarriername": null, "emporreteduinstind": null, "residenceinstypecd": null, "policycancelledwithin3yrsind": null, "declineemailind": null, "militaryserviceind": null, "clueordind": null, "otherinsurancebasicorspecialind": null, "healthinsurancecarriername": null, "healthinsurancepolicynumber": null, "transactionprogress": "1", "numofaccidentinloanedvehicle": null, "allinsuredind": null, "medicaremediaidind": null, "uwcompanycd": null, "policyissueno": null, "associatededuaffilatedorgind": null, "edocumentsind": null }, "eligibility": [], "policyagency": { "policykey": 0, "branchid": null, "agencyid": null, "producerid": null, "agencyhierid": 23155, "agentloginid": null, "username": null, "usertypecd": null, "emailaddressid": null, "referchannel": "others" }, "participants": [ { "policykey": 0, "participantid": 0, "firstname": "khagendra", "middlename": null, "lastname": "siripurapu", "suffix": null, "birthdate": null, "ssn": null, "fid": null, "gender": "", "licnum": null, "licstatecd": null, "maritalstatuscd": null, "relationshiptoinscd": "a", "businessname": null, "participantrole": "primary_insured" } ], "vehicles": [], "drivers": [ { "driverid": 0, "firstname": "khagendra", "middlename": null, "lastname": "siripurapu", "licenseatage": 0, "licensestatuscode": null, "defensedrivercourseindicator": null, "goodstudentindicator": null, "drivertrainingindicator": null, "awayatschoolindicator": null, "incidents": null, "occupation": null, "insuredonother": null, "continuouscoverageindicator": null, "priorlicensenumber": null, "drivercoursedate": null, "drivercoursename": null, "firstlicensenonusa": null, "awayatmilitaryindicator": null, "sr22filingindicator": null, "otherpolicycarriername": null, "awayatschoolcomment": null, "licenseoosprior3yrsind": "n", "licenseoosprior3yearsstatecd": null, "advdrivertrainingdate": null, "defensivedrivercoursedate": null, "otherpolicytypecode": null, "accidentprvntcoursedate": null, "phonenumber": null, "safetypledgeind": null, "accidents": null, "violations": null } ], "priorcarrier": { "priorbilimits": null, "priorcarrname": null, "policykey": 0, "continuouscoveragedesc": null, "lapsedreason": null, "numofyearofcontinuouscoverage": null, "activecurrentpoilcyind": null }, "discounts": null, "companionpolicy": null, "payment": null }, "allinsuredind": "y", "medicaremediaidind": "y", "driversgreaterthan65": true }
pojo class :
public class ratingdmo implements serializable { private static final long serialversionuid = -433016040176969496l; private string company; private string state; private string lob; private string channel; private string username; private string password; private string policynumber; private long policykey; private string transactiontype; private string evaluationtype; private string clientid; private string policyterm; private string effectivedate; private string policyxml; private string webxml; private string firstname; private string lastname; private string businessname; private string city; private string zip; private string effectivedtstart; private string effectivedtend; private string searchtype; private string uuid; private list<string> policystatuscodes; private map<string,string> eadvisorcoverages; private map<string,string> customcoverages; private map<string,string> defaultcoverages; private string action; private string type; private string selectedcoverage; private boolean motorclubmember; private string selectedsubcoveres; private boolean customrated; private final policydmo policydmo; private final string allinsuredind; private final string medicaremediaidind; private final boolean driversgreaterthan65; public policyratingrequestdmo(policydmo policydmo, string allinsuredind, string medicaremediaidind, boolean driversgreaterthan65){ this.policydmo = policydmo; this.allinsuredind = allinsuredind; this.medicaremediaidind = medicaremediaidind; this.driversgreaterthan65 = driversgreaterthan65; } ... }
by default jackson's data binding try construct object using no-argument constructor , fill in object's properties corresponding bits of json. can override behaviour annotations
@jsoncreator public policyratingrequestdmo(@jsonproperty("policyinfo") policydmo policydmo, @jsonproperty("allinsuredind") string allinsuredind, @jsonproperty("medicaremediaidind") string medicaremediaidind, @jsonproperty("driversgreaterthan65") boolean driversgreaterthan65){ this.policydmo = policydmo; this.allinsuredind = allinsuredind; this.medicaremediaidind = medicaremediaidind; this.driversgreaterthan65 = driversgreaterthan65; }
the jsoncreator
annotation tells jackson constructor call, , parameters annotated jsonproperty
indicate properties in json correspond parameters in constructor. additional properties in json not correspond constructor arguments injected via setters or direct field access normal.
Comments
Post a Comment