[英]Jaxb2Marshaller can not serialize POJO by xsd schema: “cannot find the declaration of element”
我有一些xsd模式example.xsd,在其中創建了pojo類(由Jaxb IntellIj Idea自動嘗試,或手動嘗試)。
我的目的是為我的spring jms服務序列化pojo並在發送之前針對xsd驗證一個。
//init marshaller
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
....
marshaller.setSchema(new ClassPathResource("/example.xsd"));
marshaller.afterPropertiesSet();
//trying to serialize
MyResponse res = new MyResponse();
Jaxb2Marshaller jaxb2Marshaller = jaxb2MarshallerGenerated();
OutputStream outputStream = new ByteArrayOutputStream();
jaxb2Marshaller.marshal(res, new StreamResult(outputStream));
我的XSD看起來像
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="unqualified" elementFormDefault="qualified"
targetNamespace="http://example/schemas">
<xs:element xmlns:sch="http://example/schemas" name="myResponse" type="sch:myResponseType"/>
<xs:complexType name="myResponseType">
<xs:sequence>
....
</xs:sequence>
....
我的POJO類如下:
@XmlRootElement
public class MyResponse {
//some jaxb stuff
}
我無法擺脫異常:
找不到元素'myResponse的聲明
我嘗試了沒有名稱空間和其他方式。
問題是我序列化為xml的POJO在根元素上沒有名稱空間。
XSD->在Idea修復一個創建Java類
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.