2017-08-11 20 views
1

私はpojoクラスを作成したxsd schema example.xsdをいくつか持っています(Jaxb IntellIj Ideaで自動試してみました)。Jaxb2MarshallerはxsdスキーマでPOJOをシリアル化できません: "要素の宣言が見つかりません"

私はspringjmsサービスのために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

名前空間などの方法を使わずに試しました。

答えて

0

問題は、POJOがxmlにシリアライズされていて、ルート要素に名前空間がないことです。

XSD - >アイデアでJavaクラスを作成する

+0

あなたは何を意味するのでしょうか教えてください。生成されたクラスに何かを追加する必要がありますか?私も同じ問題がありますが、私はIntelliJを使用していません。 – Line

+1

@Lineはい、生成されたクラスは、名前空間を含める必要があります。 ..XmlType(名前= "someNameという"、名前空間= "yourNameSpace") を、各要素はべきものが含まれます ..XmlElement(名前= "someName2"、 namespace = "yourNameSpace").. –

+0

OKです。ありがとうございますが、パッケージレベルではなく名前を指定したいと思います。もう1つは、あなた自身の答えを受け入れるべきです;)https://stackoverflow.com/help/self-answer – Line

関連する問題