簡體   English   中英

CXF的JAXB模式驗證錯誤-發生JAXBException:cvc-elt.1:找不到元素的聲明

[英]JAXB schema validation error with CXF - JAXBException occurred : cvc-elt.1: Cannot find the declaration of element

我有一個使用CXF 3.0.1REST服務,該服務在HTTP POST有效負載中接受XML消息。 JAXBXML有效負載解組到對象。

我正在嘗試通過XSD架構驗證XML ,並且已經在CXF中配置了XSD ,但是我一直收到以下錯誤

發生JAXBException:cvc-elt.1:找不到元素'incident'的聲明。.cvc-elt.1:找不到元素'incident'的聲明。

注意:事件是我的根本要素

據我了解, XSD已由CXF成功注冊,但JAXB方面出了點問題。

我嘗試了許多與該錯誤相關的解決方案,但均無效果。

有任何想法嗎

謝謝

這是我的配置

服務

@Path("incident") 
public class CreateIncident { 
        @POST 
        @Consumes({ MediaType.APPLICATION_XML}) 
        public Response createIncident(Incident incident) { 
                        //code 
        } 
}

JAXB對象

@XmlRootElement(name = "incident") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Incident extends Event { 
        public Incident() { 
                super("incident"); 
        } 

        @XmlElement
        private String importProfile;

        @XmlElement
        private String eventTitle; 

        public String getImportProfile() { 
                return importProfile; 
        } 

        public void setImportProfile(String importProfile) { 
                this.importProfile = importProfile; 
        } 

        public String getEventTitle() {
                return eventTitle;
        }

        public void setEventTitle(String eventTitle) {
                this.eventTitle = eventTitle;
        }
}

事件:

public class Event {

    String eventType;

    public Event(String eventType) {
        this.eventType = eventType;
    }

    public String getEventType(){
        return eventType;
    }
}

我的XSD

<?xml version="1.0" encoding="UTF-8"?>
<schema
    xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.ba.com/schema/BAassystWrapper/incident"
    elementFormDefault="qualified">
    <element name="incident">
        <complexType>
            <sequence>
                <element name="importProfile">
                    <simpleType>
                        <restriction base="string">
                            <minLength value="1"></minLength>
                            <maxLength value="254"></maxLength>
                        </restriction>
                    </simpleType>
                </element>
                <element name="eventTitle">
                    <simpleType>
                        <restriction base="string">
                            <minLength value="1"></minLength>
                            <maxLength value="890"></maxLength>
                        </restriction>
                    </simpleType>
                </element>
            </sequence>
        </complexType>
    </element>
</schema>

我通過的XML

<incident>
    <importProfile>Test text</importProfile>
    <eventTitle>Test text</eventTitle>
</incident>

CXF配置

<jaxrs:server address="/">
                <jaxrs:schemaLocations>
                        <jaxrs:schemaLocation>classpath:xsd/incident.xsd</jaxrs:schemaLocation>
                </jaxrs:schemaLocations>
                <jaxrs:serviceBeans>
                        <bean class="com.ba.sysman.services.events.CreateIncident"></bean>
                </jaxrs:serviceBeans>
                <jaxrs:features>
                        <cxf:logging/>
                </jaxrs:features>
</jaxrs:server>

根元素必須是名稱空間限定的。 因此,傳入的XML需要類似於:

<incident xmlns="http://www.ba.com/.......">

由於尚未在@XmlRootElement中定義名稱空間,因此請從輸入中刪除名稱空間。

@XmlRootElement(name = "incident") 
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({Event.class}) 
public class Incident extends Event { 
        public Incident() { 
                super("incident"); 
        }

    //fields, getters and setters
}

並在事件類中添加默認構造函數

public Event(){

}

但是,最好使用xjc pluginwadl2java/wsdl2java plugin並從xsd生成jaxb類,這將在您頻繁更改xsd並使用xmlns時節省大量時間

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM