使用com.fasterxml.jackson.databind无法识别的字段

vdzxcuhz  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(428)

我有一个具有以下结构的xsd文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns="someurl" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="someurl" 
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Accesso">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Identificativo" type="idAccesso"/>
            <xs:element ref="Erogatore"/>
            <xs:element ref="Entrata"/>
            <xs:element name="ModalitaArrivo" type="modalitaArrivo"/>
            <xs:element ref="CentraleOperativa" minOccurs="0"/>
            <xs:element name="ResponsabileInvio" type="responsabileInvio"/>
            <xs:element name="IstitutoProvenienza" type="istitutoProvenienza" minOccurs="0"/>
            <xs:element name="ProblemaPrincipale" type="problemaPrincipale"/>
            <xs:element name="Trauma" type="trauma" minOccurs="0"/>
            <xs:element name="Triage" type="triageAccesso"/>
            <xs:element ref="Assistito"/>
            <xs:element ref="Importo" minOccurs="0"/>
            <xs:element name="TipoTrasmissione" type="tipoTrasmissione"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:element name="Assistito">
    ... xml code ...
</xs:element>
<xs:element name="CentraleOperativa">
    ... xml code ...
</xs:element>
<xs:element name="flsProSoc">
    <xs:complexType mixed="true">
        <xs:choice>
            <xs:element ref="Accesso" minOccurs="0" maxOccurs="unbounded"/>
        </xs:choice>
    </xs:complexType>
</xs:element>

... others ref fields ...
</xs:schema>

所以,我用jaxb2生成了java类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
   "content"
})
@XmlRootElement(name = "flsProSoc")
public class FlsProSoc {

    @XmlElementRef(name = "Accesso", namespace = "someurl", type = Accesso.class, required = false)
    @XmlMixed
    protected List<Object> content;

    public List<Object> getContent() {
        if (content == null) {
            content = new ArrayList<Object>();
        }
        return this.content;
    }
}

accesso类和其他类也一样。这是我试图反序列化的xml文件的一个示例:

<?xml version="1.0"?>
<flsProSoc xmlns="url" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Accesso>
     <Identificativo></Identificativo>
     <Erogatore>... xml fields ...</Erogatore>
     <Entrata>... xml fields ...</Entrata>
     <ModalitaArrivo></ModalitaArrivo>
     <CentraleOperativa>... xml fields ...</CentraleOperativa>
     <ResponsabileInvio></ResponsabileInvio>
     <ProblemaPrincipale></ProblemaPrincipale>
     <Triage></Triage>
     <Assistito>... xml fields ...</Assistito>
     <Importo>... xml fields ...</Importo>
     <TipoTrasmissione>I</TipoTrasmissione>
 </Accesso>

 <Accesso> ... </Accesso>
 <Accesso> ... </Accesso>
 <Accesso> ... </Accesso>
 <Accesso> ... </Accesso>

但当我要反序列化某个xml文件时,会出现以下错误:
com.fasterxml.jackson.databind.exc.unrecognedPropertyException:未识别的字段“accesso”(类com.xx.yy.flsprosoc),未标记为可忽略(一个已知属性:“content”]),位于[源:(文件);行:4,列:21](通过引用链:com.xx.yy.flsprosoc[“accesso”])
我在gradle用这个

compile 'com.fasterxml.jackson.core:jackson-core:2.11.0'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.11.0'
compile 'com.fasterxml.jackson.core:jackson-databind:2.11.0'
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.11.0'
compile 'org.codehaus.woodstox:woodstox-core-asl:4.4.1'

我试过使用:

(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

有了这一点也不例外,但我也没有从我的xml中获得任何数据。有人能帮忙解决这个问题吗?

3zwjbxry

3zwjbxry1#

解决方案正在使用

@JsonProperty("Accesso")

就在flsprosoc类中的getcontent()方法之前。

相关问题