码迷,mamicode.com
首页 > 其他好文 > 详细

The attribute required is undefined for the annotation type XmlElementRef

时间:2018-10-09 12:17:33      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:inter   not   不同的   name   代码   分代   mes   sch   imp   

异常描述:
几天没用的项目导进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

标签:inter   not   不同的   name   代码   分代   mes   sch   imp   

原文地址:https://www.cnblogs.com/firstdream/p/9759510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!