ObjectMapper的坑

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并不会报错,而是直接精度丢失了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,774评论 0 9
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,287评论 25 708
  • 有多少真心 你误以为是玩笑 所以愉快的交谈已是过往 我们还是最好的朋友 游戏青春 自认为,我们所认为的 现在 想起...
    猫眼小先生阅读 214评论 0 0
  • “二哥”这个称呼是朋友聚会的时候排下来的,叫的久了,也就自然习惯了。 仔细想想也很有意思,似乎从小到大就没被排过大...
    二哥的食堂阅读 995评论 2 9