简体   繁体   中英

Jaxb2Marshaller can not serialize POJO by xsd schema: “cannot find the declaration of element”

I have some xsd schema example.xsd on which one I created pojo classes (tried auto by Jaxb IntellIj Idea, or manually).

I have purpose to serialize pojo for my spring jms service and validate one against xsd before sending.

//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));

My xsd looks like

<?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>
....

My POJO classes looks like:

@XmlRootElement
public class MyResponse {
//some jaxb stuff
}

I cant escape exception:

cannot find the declaration of element 'myResponse

I tried without namespace and other ways.

The problem was that my POJO serialized to xml has not namespace at root element.

XSD -> create java classes at Idea fixed one

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM