![](/img/trans.png)
[英]xsd validation fails with cvc-elt.1: Cannot find the declaration of element
[英]JAXB schema validation error with CXF - JAXBException occurred : cvc-elt.1: Cannot find the declaration of element
我有一个使用CXF 3.0.1的REST服务,该服务在HTTP POST有效负载中接受XML消息。 JAXB将XML有效负载解组到对象。
我正在尝试通过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 plugin
和wadl2java/wsdl2java plugin
并从xsd生成jaxb类,这将在您频繁更改xsd并使用xmlns时节省大量时间
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.