【发布时间】:2019-02-24 00:50:43
【问题描述】:
所有, 我正在使用杰克逊 2.9.5
我正在尝试解析 Blazemeter Taurus 的 final-stats 模块的 XML 输出。
有一个 XML 元素“Group”,其子元素“perc”表示百分位数。 Perc 有一个未映射的“名称”子级。我认为应该忽略“名字”孩子,因为如果我删除@JsonIgnorePropertues(ignoreUnknown=true),杰克逊会爆炸,因为它无法识别“名字”。
但是,在反序列化 Group 时,名称不会被忽略。相反,我得到了
com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造
com.mycompany.myproject.Percentile的实例(尽管至少存在一个创建者):没有从字符串值反序列化的字符串参数构造函数/工厂方法('perc/90.0' ) 在 [来源:(BufferedInputStream);行:4,列:20](通过引用链:com.mycompany.myproject.Group["perc"]->java.util.ArrayList[0])
“perc/90.0”是未映射的“name”元素的值。
更奇怪的是,当我尝试仅反序列化 perc 元素时,它工作正常。
这是失败的 XML:
<?xml version='1.0' encoding='UTF-8'?>
<Group label="https://myhost.mycompany.com:8443/login?from=%2F">
<perc value="0.19300" param="90.0">
<name>perc/90.0</name>
</perc>
</Group
这是我的反序列化代码:
XmlMapper mapper = new XmlMapper();
mapper.registerModule(new JaxbAnnotationModule());
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("taurus/group-small.xml");
Group group = mapper.readValue(in, Group.class);
此 XML 有效:
<perc value="0.19300" param="90.0">
<name>perc/90.0</name>
</perc>
使用这个反序列化代码:
XmlMapper mapper = new XmlMapper();
mapper.registerModule(new JaxbAnnotationModule());
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("taurus/percentile2.xml");
Percentile p = mapper.readValue(in, Percentile.class);
这些是我的映射类:
package com.mycompany.myproject;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
@XmlRootElement(name="perc")
public class Percentile {
@XmlAttribute(name="value")
private double value;
@XmlAttribute(name="param")
private double name;
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public double getName() {
return name;
}
public void setName(double name) {
this.name = name;
}
}
package com.mycompany.myproject;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
@XmlRootElement(name="Group")
public class Group {
@XmlAttribute(name="label")
private String label;
@XmlElement(name="perc")
private List<Percentile> responseTimePercentiles;
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public List<Percentile> getResponseTimePercentiles() {
return responseTimePercentiles;
}
public void setResponseTimePercentiles(List<Percentile> responseTimePercentiles) {
this.responseTimePercentiles = responseTimePercentiles;
}
}
谢谢
【问题讨论】: