zoukankan      html  css  js  c++  java
  • The attribute required is undefined for the annotation type XmlElementRef

    异常描述:
    几天没用的项目导进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)也是可以满足要求的
    
  • 相关阅读:
    北京大学数学分析习题集参考解答03.07难题
    北京大学数学分析习题集参考解答03.06一致连续性
    北京大学数学分析习题集参考解答03.05最大、最小值
    北京大学数学分析习题集参考解答03.03中间值性质03.04初等函数的连续性
    北京大学数学分析习题集参考解答03.02连续函数的运算
    北京大学数学分析习题集参考解答03.01连续与间断
    [Oracle工程师手记] 利用 DBMS_SQLTUNE.report_sql_monitor 生成 SQL 语句的监控信息
    [Oracle工程师手记] Data Guard 环境中,查找最近发生的与 Data Guard 相关的错误的方法
    [Oracle 工程师手记] Windows 环境下,获取与 oracle 相关 registry 的小技巧
    [Oracle工程师手记]从RAC环境备份后向新环境(文件系统)恢复的试验
  • 原文地址:https://www.cnblogs.com/firstdream/p/9759510.html
Copyright © 2011-2022 走看看