Jaxb2Marshaller无法通过xsd模式序列化POJO:“无法找到元素的声明”
问题描述:
我有一个xsd模式example.xsd,我创建了pojo类(由Jaxb IntellIj Idea尝试自动,或手动)。Jaxb2Marshaller无法通过xsd模式序列化POJO:“无法找到元素的声明”
我有目的序列化我的弹簧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
我试过没有命名空间和其他方式。
答
问题是我的POJO序列化到xml没有名称空间在根元素。
XSD - >创建的理念Java类固定一个
能否请您解释一下是什么意思?需要将某些东西添加到生成的类中?我有同样的问题,但我没有使用IntelliJ。 – Line
@Line是,生成的类应包含名称空间: ..XmlType(名称= “someName”,命名空间= “yourNameSpace”) 和各元件应包含一个: ..XmlElement(名称= “someName2”, namespace =“yourNameSpace”).. –
好的。谢谢,但我想指定名称而不是包级别。还有一件事,你应该接受你自己的答案;)https://*.com/help/self-answer – Line