Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jaxb2Marshaller无法通过xsd架构序列化POJO:";找不到元素“的声明”;_Java_Xml_Intellij Idea_Xsd_Jaxb - Fatal编程技术网

Java Jaxb2Marshaller无法通过xsd架构序列化POJO:";找不到元素“的声明”;

Java Jaxb2Marshaller无法通过xsd架构序列化POJO:";找不到元素“的声明”;,java,xml,intellij-idea,xsd,jaxb,Java,Xml,Intellij Idea,Xsd,Jaxb,我有一些XSDSchema example.xsd,我在其中创建了pojo类(由Jaxb IntellIj Idea自动尝试,或手动尝试) 我的目的是为我的SpringJMS服务序列化pojo,并在发送之前根据xsd验证pojo //init marshaller Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); .... marshaller.setSchema(new ClassPathResource("/example.xsd"));

我有一些XSDSchema example.xsd,我在其中创建了pojo类(由Jaxb IntellIj Idea自动尝试,或手动尝试)

我的目的是为我的SpringJMS服务序列化pojo,并在发送之前根据xsd验证pojo

//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>
....
我无法逃避例外:

找不到元素“myResponse”的声明


我尝试不使用名称空间和其他方法。

问题是,序列化为xml的POJO在根元素处没有名称空间


XSD->在Idea fixed one上创建java类

问题是序列化为xml的POJO在根元素上没有名称空间


XSD->在Idea fixed one创建java类

您能解释一下您的意思吗?需要将某些内容添加到生成的类中吗?我也有同样的问题,但我没有使用IntelliJ。@行是的,生成的类应该包含名称空间:…XmlType(name=“someName”,namespace=“yourNameSpace”),每个元素应该包含一个:…xmlement(name=“someName2”,namespace=“yourNameSpace”)…确定。谢谢,但是我想在包级别指定名称。还有一件事,你应该接受你自己的答案;)你能解释一下你的意思吗?需要将某些内容添加到生成的类中吗?我也有同样的问题,但我没有使用IntelliJ。@行是的,生成的类应该包含名称空间:…XmlType(name=“someName”,namespace=“yourNameSpace”),每个元素应该包含一个:…xmlement(name=“someName2”,namespace=“yourNameSpace”)…确定。谢谢,但是我想在包级别指定名称。还有一件事,你应该接受你自己的答案;)
@XmlRootElement
public class MyResponse {
//some jaxb stuff
}