JSON需要属性vs无法识别

时间:2018-01-23 18:27:09

标签: java json

我的java对象有foll。 JSON属性

@JsonProperty(required = true)
private String taxGroupingId;

我没有将@JsonIgnoreProperties(ignoreUnknown = true)给予java对象/类。

JSON消息如下所示,

{"taxPayerId":27667865,"SSN":"384732897687639","taxPayerName":"ABS","taxPayerId":"23203948"}

现在抛出的异常是无法识别的字段" taxPayerId"但我期望的是与#34;必填字段缺失相关的异常"。 我应该使用哪个属性来获得所需的异常。

package com.usps.data.pricing.vo;


import com.fasterxml.jackson.annotation.JsonProperty;


public class TaxValuation {

    @JsonProperty(required = true)
    private String taxGroupingId;

    private String SSN;

    private String taxPayerName;

    private String taxPayerId;

    /**
     * @return the taxGroupingId
     */
    public String getTaxGroupingId() {
        return taxGroupingId;
    }

    /**
     * @param taxGroupingId the taxGroupingId to set
     */
    public void setTaxGroupingId(String taxGroupingId) {
        this.taxGroupingId = taxGroupingId;
    }

    /**
     * @return the sSN
     */
    public String getSSN() {
        return SSN;
    }

    /**
     * @param sSN the sSN to set
     */
    public void setSSN(String sSN) {
        SSN = sSN;
    }

    /**
     * @return the taxPayerName
     */
    public String getTaxPayerName() {
        return taxPayerName;
    }

    /**
     * @param taxPayerName the taxPayerName to set
     */
    public void setTaxPayerName(String taxPayerName) {
        this.taxPayerName = taxPayerName;
    }

    /**
     * @return the taxPayerId
     */
    public String getTaxPayerId() {
        return taxPayerId;
    }

    /**
     * @param taxPayerId the taxPayerId to set
     */
    public void setTaxPayerId(String taxPayerId) {
        this.taxPayerId = taxPayerId;
    }



}

2 个答案:

答案 0 :(得分:0)

尝试将@JsonDeserialize添加到课程中。

答案 1 :(得分:0)

I have not given @JsonIgnoreProperties(ignoreUnknown = true) to the java object/class

While the documentation of ignoreUnknown uses a bit cryptic wording, it still says that the default behaviour is to throw the exception you see, upon encountering an unknown field. Since "taxPayerId" is the very first thing the parser encounters, it just throws the exception.
Checking for the presence of all required fields could take place after finishing to parse the object, in a state which the parser simply does not reach with your current code.