1. 程式人生 > >The attribute required is undefined for the annotation type XmlElementRef

The attribute required is undefined for the annotation type XmlElementRef

生成 bool target sta div val 方式 java 需要

異常描述:
幾天沒用的項目導進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;
......
@XmlElementRef那行報錯:The attribute required is undefined for the annotation type XmlElementRef
 
異常分析:
剛開始有點摸不到頭緒,因為這部分代碼是使用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;
......
項目當前使用JDK1.6.0_45,於是看了下JDK1.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屬性,換成JDK1.7.0_65之後,再看下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 {}

    /**
     * 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的內容從JDK1.6開始,已經被添加到rt.jar中了 

解決方式:
如果需要required屬性,可將JDK換為1.7,如果不需要,將JDK換為1.6的,再使用JAXB重新生成代碼即可
當然了,如果必須要用JDK1.6,而且需要支持required屬性,那麽JDK1.6 + jaxb-api(我使用的是jaxb-api-2.2.1.jar,僅僅加入jaxb-api-2.2.1.jar代碼便不會報錯了,但是如果想使用JAXB生成代碼,僅僅只有api是不夠的,還需要其它相關jar,比如:jaxb-impl-2.2.1.jar)也是可以滿足要求的

The attribute required is undefined for the annotation type XmlElementRef