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)也是可以满足要求的

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值