2016-08-25 6 views
0

は、私は、Javaを生成していますと、JAXBを持つオブジェクト:XmlAdapterを使用してXML複合型をスキーマ生成クラスのJavaオブジェクトにマップする方法この(デモ)スキーマを使用して

@XmlType 
public class SomeType { 

    @XmlElement(name = "myOtherType") 
    OtherType myOtherType; 

} 

しかし、私は私のJAXB-に代わりの実装のインタフェースを使用したいと思います:

<xsd:complexType name="someType"> 
    <xsd:sequence> 
     <xsd:element name="myOtherType" type="otherType" maxOccurs="unbounded" /> 
    </xsd:sequence> 
</xsd:complexType> 

<xsd:complexType name="otherType"> 
    <!-- ... --> 
</xsd:complexType> 

このクラスが生成されます生成されたオブジェクト。

は、だから私は、このインタフェース記述:

public interface OtherTypeInterface { 
    // .... 
} 

をそして私は、生成さOtherTypeクラスのバインドファイルの助けを借りて、それを実装してみましょう:

<jxb:bindings node="//xs:complexType[@name='otherType']"> 
    <inheritance:implements>com.example.OtherTypeInterface</inheritance:implements> 
</jxb:bindings> 

これまでのところ、とても良い:

public class OtherType implements OtherTypeInterface { 

    // ... 

} 

しかし、OtherType実装ではなく、このインタフェースを使用するにはSomeTypeオブジェクトが必要です。示唆されているように、セクションin the unofficial JAXB guide 3.2.2。使用@XmlJavaTypeAdapter、私はそのインターフェイスにOtherTypeをマップするために自家製XMLアダプタを使用したい、またはその逆:

public class HcpartyTypeAdapter extends XmlAdapter<OtherType, OtherTypeInterface> { 

    @Override 
    public OtherTypeInterface unmarshal(OtherType v) throws Exception { 
     return v; 
    } 

    @Override 
    public OtherType marshal(OtherTypeInterface v) throws Exception { 
     return (OtherType) v; 
    } 

} 

しかし、それは次のように構成された私のバインディングファイル内のXML複合タイプをマッピングするように見えます

<jxb:globalBindings> 
    <xjc:javaType name="com.example.OtherTypeInterface" xmlType="ex:otherType" adapter="com.example.OtherTypeAdapter"/> 
</jxb:globalBindings> 

世代がこのエラーで失敗します:無大したではありません

com.sun.istack.SAXParseException2; systemId: file:/.../bindings.xjb; lineNumber: 8; columnNumber: 22; undefined simple type "{ http://www.example.com }otherType".

a bit of googlingで、私はその私を発見しましたスキーマ生成クラスの複雑な型に対してXMLアダプタを使用することは明らかに不可能です。私は手動で私のアダプタを使用するファイルを編集していた場合しかし、それは完璧に動作します:

public class SomeType { 

    @XmlElement(name = "myOtherType") 
    @XmlJavaTypeAdapter(OtherTypeAdapter.class) 
    @XmlSchemaType(name = "otherType") 
    OtherTypeInterface myOtherType; 

} 

私は完璧にそれをマーシャリングし、非整列化することができます。生成されたクラスを編集することは、自動処理の全目的を破るように思えます。私は多くの多くの型を定義する複数のスキーマを扱っています。

私の質問は、手動でコードを編集せずに、XMLコンポジット型をスキーマ生成クラスのJavaオブジェクトにマップするための回避策を知っている人は誰ですか?


ここでの回答はhttps://stackoverflow.com/a/1889584/946800です。私は2009年以来、誰かがこの問題を解決する何らかの方法を見つけた可能性があります。

答えて

0

mavenプラグインを使用して、生成されたJAXBクラスに注釈を追加することができます。 .xjbバインディングで

  <plugin> 
       <!-- this plugin is used to add annotation for the models --> 
       <groupId>org.jvnet.jaxb2_commons</groupId> 
       <artifactId>jaxb2-basics-annotate</artifactId> 
       <version>1.0.2</version> 
      </plugin> 

<jxb:bindings schemaLocation="sample.xsd"> 
    <jxb:bindings node="//xs:complexType[@name='otherType']"> 
     <annox:annotate target="field"> 
      <annox:annotate annox:class="@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter" 
       value="com.example.OtherTypeAdapter.class" /> 
     </annox:annotate> 
    </jxb:bindings> 
</jxb:bindings> 
関連する問題