美文网首页
ObjectMapper的坑

ObjectMapper的坑

作者: 金字塔底的蜗牛 | 来源:发表于2017-07-07 14:10 被阅读0次

2017.07.07

报错信息

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "conditionQuantity" (class objectMapper.NewInteger), not marked as ignorable (7 known properties: "reduceFee", "perReduceFee", "priority", "match", "reduceDiscount", "conditionFee", "per"])
 at [Source: [{"conditionQuantity":30,"reduceDiscount":90.5,"priority":1}]; line: 1, column: 25] (through reference chain: java.util.ArrayList[0]->objectMapper.NewInteger["conditionQuantity"])

一直显示的错误是没有匹配的conditionQuantity字段

Class源码

public class NewInteger implements Serializable{

    private static final long serialVersionUID = 4878434867152140447L;

    private Long conditionFee;      

    private Long per;               

    private Long perReduceFee;       

    private Integer conditionQuantity;  

    private Long reduceFee;         

    private Integer reduceDiscount;     

    private Integer priority;           

    private Boolean isMatch;           


}

很明显class类中是有conditionQuantity字段的,于是思考过去成功的案例。

发现唯一失败的可能就是没有设置setter和getter方法。

于是修改class类的代码。

修改后Class

public class NewInteger implements Serializable{

    private static final long serialVersionUID = 4878434867152140447L;

    private Long conditionFee;     

    private Long per;                

    private Long perReduceFee;       

    private Integer conditionQuantity;  

    private Long reduceFee;         

    private Integer reduceDiscount;    

    private Integer priority;           

    private Boolean isMatch;          


    public Long getConditionFee() {
        return conditionFee;
    }

    public void setConditionFee(Long conditionFee) {
        this.conditionFee = conditionFee;
    }

    public Long getPer() {
        return per;
    }

    public void setPer(Long per) {
        this.per = per;
    }

    public Long getPerReduceFee() {
        return perReduceFee;
    }

    public void setPerReduceFee(Long perReduceFee) {
        this.perReduceFee = perReduceFee;
    }

    public Integer getConditionQuantity() {
        return conditionQuantity;
    }

    public void setConditionQuantity(Integer conditionQuantity) {
        this.conditionQuantity = conditionQuantity;
    }

    public Long getReduceFee() {
        return reduceFee;
    }

    public void setReduceFee(Long reduceFee) {
        this.reduceFee = reduceFee;
    }

    public Integer getReduceDiscount() {
        return reduceDiscount;
    }

    public void setReduceDiscount(Integer reduceDiscount) {
        this.reduceDiscount = reduceDiscount;
    }

    public Integer getPriority() {
        return priority;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    public Boolean getMatch() {
        return isMatch;
    }

    public void setMatch(Boolean match) {
        isMatch = match;
    }
}

ObjectMapper解析代码

public class ObejctMapperTest {


    public List<NewInteger> parse() throws IOException {

        ObjectMapper objectMapper = new ObjectMapper();

        String json = "[{\"conditionQuantity\":30,\"reduceDiscount\":90.5,\"priority\":1}]";
        return objectMapper.readValue(json, new TypeReference<List<NewInteger>>(){});


    }

    public static void main(String[] args) {
        ObejctMapperTest run = new ObejctMapperTest();
        try {
            System.out.println("before parse");
            List<NewInteger> result = run.parse();
            System.out.println(result.get(0).getReduceDiscount());
        } catch (IOException e) {
            System.out.println("catch parse error");
            System.out.println(e.toString());
        }
    }
}

[解析结果]

解析结果

从结果来看,很明显踩到另一个坑了。
在拿Integer类型的字段reduceDiscount时,相对的实际参数是90.5,这种情况下,objectMapper并不会报错,而是直接精度丢失了。

相关文章

网友评论

      本文标题:ObjectMapper的坑

      本文链接:https://www.haomeiwen.com/subject/lwilhxtx.html