发生 CXF 的 JAXB 模式验证错误 - 发生 JAXBException:cvc-elt.1:找不到元素的声明



我有一个带有CXF 3.0.1REST服务,它接受HTTP POST有效负载中的XML消息。XML 有效负载正在由 JAXB 解编到对象。

我正在尝试通过 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/.......">

由于您尚未在 中定义 nameapce,因此@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 时,这将节省大量时间

最新更新