为什么Json要求Junit测试没有参数构造函数?

时间:2014-08-18 16:33:10

标签: java json junit jackson

我在使用Jackson反序列化JSON字符串时遇到问题。我收到错误:com.fasterxml.jackson.databind.JsonMappingException:找不到类型[simple type,class RatingDMO]的合适构造函数:无法从JSON对象实例化(需要添加/启用类型信息?)

我将字段映射到JSON文件,并且它的参数可用作属性并且具有相同的类型。

注意: - 如果我提供no-arg构造函数(仅用于检查它是否有效),则测试有效。

我无法在代码中提供/保留默认构造函数,因为模型中的最终字段很少。

mapper.readValue(new File(" ratingRequest.json"),RatingDMO.class);

JSON文件: -

{
    "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班级:

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;
    }
... }

1 个答案:

答案 0 :(得分:8)

默认情况下,Jackson的数据绑定将尝试使用无参数构造函数构造对象,然后从JSON的相应位填充对象的属性。您可以使用注释覆盖此行为

@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;
}

JsonCreator注释告诉Jackson要调用哪个构造函数,并使用JsonProperty注释其参数,以指示JSON中哪些属性对应于构造函数中的哪些参数。 JSON中与构造函数参数不对应的其他属性将通过setter或直接字段访问注入。