The atribute required is undefined for the annotation type Xml Element Ref

4043 ワード

異常な説明:
何日間の無駄なプロジェクトをEclipseに誘導したら異常があります。
public class BooleanFeatureType
    extends FeatureBaseType{

    @XmlElementRef(name = "Value", namespace = "http://schemas.sean.com/ma/CA/OPM/", 
			type = JAXBElement.class, required = false)
    protected JAXBElement<Boolean> value;
......
@Xml Element Ref行のエラー:The atribute required is undefined for the annotation type Xml Element Ref
 
異常分析:
最初はちょっと分かりませんでした。この部分のコードはJAXBを使ってschemaによって自動的に生成されます。schemaは次のように定義されています。
<xsd:element name="BooleanFeature" type="BooleanFeatureType" minOccurs="0" 
		maxOccurs="unbounded">
	<xsd:annotation>
		<xsd:documentation>Boolean feature.</xsd:documentation>
	</xsd:annotation>
</xsd:element>
<xsd:complexType name="BooleanFeatureType">
	<xsd:annotation>
        <xsd:documentation>Type for features with a boolean (true or false) value.
		</xsd:documentation>
    </xsd:annotation>
    <xsd:complexContent>
		<xsd:extension base="FeatureBaseType">
			<xsd:sequence>
               <xsd:element name="Value" type="xsd:boolean" nillable="true" minOccurs="0">
                  <xsd:annotation>
                     <xsd:documentation>The feature value, true or false.</xsd:documentation>
                  </xsd:annotation>
               </xsd:element>
            </xsd:sequence>
         </xsd:extension>
	</xsd:complexContent>
</xsd:complexType>
再生成を試みましたが、再生成されたコードにはrequired属性がないことが分かりました。
public class BooleanFeatureType
    extends FeatureBaseType{
	
    @XmlElementRef(name = "Value", namespace = "http://schemas.sean.com/ma/CA/OPM/", 
			type = JAXBElement.class)
    protected JAXBElement<Boolean> value;
......
プロジェクトは現在JDK 1.6.0_を使用しています。45,そこでJDK 1.6.0_を見ました。45におけるXmlElementRefの注釈の定義
@Retention(RUNTIME)
@Target({FIELD,METHOD})
public @interface XmlElementRef {
    Class type() default DEFAULT.class;

    String namespace() default "";

    String name() default "##default";

    static final class DEFAULT {}
}
定義にはまったくrequired属性がないことが分かりました。JDK 1.7.0_に換えます。65の後、Xml ElementRefの注釈の定義を見てください。
@Retention(RUNTIME)
@Target({FIELD,METHOD})
public @interface XmlElementRef {
    Class type() default DEFAULT.class;

    String namespace() default "";

    String name() default "##default";

    static final class DEFAULT {}

    /**
     * Customize the element declaration to be required.
     * <p>
     * If required() is true, then Javabean property is mapped to
     * an XML schema element declaration with minOccurs="1".
     * maxOccurs is "1" for a single valued property and "unbounded"
     * for a multivalued property.
     *
     * <p>
     * If required() is false, then the Javabean property is mapped
     * to XML Schema element declaration with minOccurs="0".
     * maxOccurs is "1" for a single valued property and "unbounded"
     * for a multivalued property.
     *
     * <p>
     * For compatibility with JAXB 2.1, this property defaults to <tt>true</tt>,
     * despite the fact that {@link XmlElement#required()} defaults to false.
     *
     * @since 2.2
     */
    boolean required() default true;
}
主な原因はJDKバージョンの違いです。注釈の定義も違います。
required属性の注釈部分により、required属性はJAXB 2.2からXmlElementRef注釈定義に追加されたものであることが確認される。
もちろん、JAXB 2.2の内容はJDK 1.6から始まり、もうrt.jarに追加されました。
解決方法:
required属性が必要なら、JDKを1.7に変えてもいいです。必要でなければ、JDKを1.6に変えて、JAXBを使ってコードを再生成すればいいです。
もちろん、JDK 1.6を使用する必要がありますし、required属性をサポートする必要があります。JDK 1.6+jaxb-appi(私はjaxb-appi-2.21.jarを使用しています。jaxb-appi-2.11.jarコードだけを追加しても間違いはありません。JAXBコードを生成するには、apiだけでは足りません。他の関連する必要があります。