Java Jackson坚持映射未知属性
全部,, 我用的是杰克逊2.9.5 我试图解析的最终统计模块的XML输出 有一个XML元素“Group”,其子元素“perc”表示百分位数。Perc有一个未映射的“名称”子级。我认为应该忽略“name”子项,因为如果我删除Java Jackson坚持映射未知属性,java,xml,jackson,Java,Xml,Jackson,全部,, 我用的是杰克逊2.9.5 我试图解析的最终统计模块的XML输出 有一个XML元素“Group”,其子元素“perc”表示百分位数。Perc有一个未映射的“名称”子级。我认为应该忽略“name”子项,因为如果我删除@JsonIgnorePropertues(ignoreUnknown=true),Jackson会崩溃,因为它不识别“name” 但是,在反序列化组时,不会忽略名称。相反,我得到了 com.fasterxml.jackson.databind.exc.MismatchedIn
@JsonIgnorePropertues(ignoreUnknown=true)
,Jackson会崩溃,因为它不识别“name”
但是,在反序列化组时,不会忽略名称。相反,我得到了
com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造com.mycompany.myproject.Percentile
(尽管至少存在一个创建者):没有从字符串值('perc/90.0')反序列化的字符串参数构造函数/工厂方法
在[Source:(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
这种XML的工作原理是:
<perc value="0.19300" param="90.0">
<name>perc/90.0</name>
</perc>
以下是我的映射类:
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;
}
}
package com.mycompany.myproject;
导入javax.xml.bind.annotation.XmlAttribute;
导入javax.xml.bind.annotation.XmlRootElement;
导入com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown=true)
@XmlRootElement(name=“perc”)
公共类百分位数{
@XmlAttribute(name=“value”)
私人双重价值;
@XmlAttribute(name=“param”)
私人双名;
公共双getValue(){
返回值;
}
公共无效设置值(双值){
这个值=值;
}
公共双getName(){
返回名称;
}
公共无效集合名(双名){
this.name=名称;
}
}
包com.mycompany.myproject;
导入java.util.List;
导入javax.xml.bind.annotation.XmlAttribute;
导入javax.xml.bind.annotation.xmlement;
导入javax.xml.bind.annotation.XmlRootElement;
导入com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown=true)
@XmlRootElement(name=“Group”)
公共课组{
@xmltattribute(name=“label”)
私有字符串标签;
@xmlement(name=“perc”)
私有列表响应百分比;
公共字符串getLabel(){
退货标签;
}
公共void setLabel(字符串标签){
this.label=标签;
}
公共列表getResponseTimePercentiles(){
返回反应百分位数;
}
公共无效setResponseTimePercentiles(列出responseTimePercentiles){
this.responseTimePercentiles=responseTimePercentiles;
}
}
感谢您,作为替代语法的解决方法,指定了要忽略的内容,例如:
@JsonIgnoreProperties({ "internalId", "secretKey" })
这里的问题是,您的Group类定义了一个百分位数列表,该列表映射到名为
的xml列表元素。您的组xml不包含名为
的xml列表元素,而是在根xml的正下方包含
元素
由于@XmlElement(name=“perc”)
应用于列表字段,并且@XmlRootElement(name=“perc”)
也应用于单个百分比xml元素,因此您的组映射需要类似于以下内容的xml结构:
<group>
<perc/>
<perc/>
<perc/>
<perc/>
<perc/>
<group>
从Group类的列表字段中删除@xmlement(name=“perc”)
注释
这样做的最终效果是,映射不希望在XML中找到包含的列表元素,每次遇到
元素时,它都会将其添加到列表中。谢谢,但在这种情况下,我仍然会遇到相同的错误。
@JsonIgnoreProperties({ "internalId", "secretKey" })
<group>
<perc/>
<perc/>
<perc/>
<perc/>
<perc/>
<group>
<group>
<perc/>
<perc/>
<perc/>
<group>
@JsonSetter(value = "perc")
public void setPercentile(Percentile percetile) {
this.responseTimePercentiles.add(percetile);
}