[英]JAXB schema validation error with CXF - JAXBException occurred : cvc-elt.1: Cannot find the declaration of element
I have a REST service with CXF 3.0.1 which accepts an XML message in a HTTP POST payload. 我有一个使用CXF 3.0.1的REST服务,该服务在HTTP POST有效负载中接受XML消息。 The XML payload is getting unmarshalled to an Object by JAXB .
JAXB将XML有效负载解组到对象。
I am trying to validate the XML through an XSD schema and I have configured the XSD in CXF but I keep getting the error below 我正在尝试通过XSD架构验证XML ,并且已经在CXF中配置了XSD ,但是我一直收到以下错误
JAXBException occurred : cvc-elt.1: Cannot find the declaration of element 'incident'.. cvc-elt.1: Cannot find the declaration of element 'incident'.. 发生JAXBException:cvc-elt.1:找不到元素'incident'的声明。.cvc-elt.1:找不到元素'incident'的声明。
NOTE: incident is my root element 注意:事件是我的根本要素
What I understand from that is that XSD is successfully registered by CXF but something is wrong in the JAXB side. 据我了解, XSD已由CXF成功注册,但JAXB方面出了点问题。
I have tried many possible solutions related to that error but none worked. 我尝试了许多与该错误相关的解决方案,但均无效果。
Any ideas 有任何想法吗
Thanks 谢谢
Here is my configuration 这是我的配置
Service 服务
@Path("incident")
public class CreateIncident {
@POST
@Consumes({ MediaType.APPLICATION_XML})
public Response createIncident(Incident incident) {
//code
}
}
JAXB Object 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;
}
}
Event: 事件:
public class Event {
String eventType;
public Event(String eventType) {
this.eventType = eventType;
}
public String getEventType(){
return eventType;
}
}
My XSD 我的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>
The XML that I pass 我通过的XML
<incident>
<importProfile>Test text</importProfile>
<eventTitle>Test text</eventTitle>
</incident>
CXF Config 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>
THe root elements need to be namespace qualified. 根元素必须是名称空间限定的。 Thus, the incoming XML needs to be something like:
因此,传入的XML需要类似于:
<incident xmlns="http://www.ba.com/.......">
Since you have not defined namesapce in @XmlRootElement
remove the namespace from the input. 由于尚未在
@XmlRootElement
中定义名称空间,因此请从输入中删除名称空间。
@XmlRootElement(name = "incident")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({Event.class})
public class Incident extends Event {
public Incident() {
super("incident");
}
//fields, getters and setters
}
And in event class add default constructor 并在事件类中添加默认构造函数
public Event(){
}
However its better to use xjc plugin
and wadl2java/wsdl2java plugin
and generate jaxb classes from xsd, which would save lots of time when you change xsd frequently and also use xmlns 但是,最好使用
xjc plugin
和wadl2java/wsdl2java plugin
并从xsd生成jaxb类,这将在您频繁更改xsd并使用xmlns时节省大量时间
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.