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字段的,于是思考过去成功的案例。
【ObjectMapper的坑】发现唯一失败的可能就是没有设置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 parse() throws IOException {ObjectMapper objectMapper = new ObjectMapper(); String json = "[{\"conditionQuantity\":30,\"reduceDiscount\":90.5,\"priority\":1}]"; return objectMapper.readValue(json, new TypeReference>(){}); }public static void main(String[] args) { ObejctMapperTest run = new ObejctMapperTest(); try { System.out.println("before parse"); List result = run.parse(); System.out.println(result.get(0).getReduceDiscount()); } catch (IOException e) { System.out.println("catch parse error"); System.out.println(e.toString()); } } }

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

    推荐阅读