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

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

时间: 2024-10-07 18:12:09

The attribute required is undefined for the annotation type XmlElementRef的相关文章

CXF wsdl2java : The attribute required is undefined for the annotation type XmlElementRef

cxf 根据wsdl 文件生成客户端Java类 下载:apache-cxf-2.7.5 1.打开cmd 进入 apache-cxf-2.7.5\bin 2.输入 wsdl2java -p "生成类的包名" - client "wsdl地址" 例如:(wsdl2java.bat -frontend jaxws21  -p "com.test" -client  "http://192.168.3.105/services/Msp?wsdl

Annotation Type @bean,@Import,@configuration使用--官方文档

@Target(value={METHOD,ANNOTATION_TYPE}) @Retention(value=RUNTIME) @Documented public @interface Bean Indicates that a method produces a bean to be managed by the Spring container. Overview The names and semantics of the attributes to this annotation

Attribute name &quot;aphmodel&quot; associated with an element type &quot;mxg&quot; must be followed by the &#39; = &#39; charac

1.错误描述 org.apache.batik.transcoder.TranscoderException: null Enclosed Exception: Attribute name "aphmodel" associated with an element type "mxg" must be followed by the ' = ' character. at org.apache.batik.transcoder.XMLAbstractTransco

Net的wsdl生成webservice 异常:undefined simple or complex type &#39;soapenc:Array&#39;

错误代码如下: E:\>wsimport -keep service.xml parsing WSDL... [WARNING] src-resolve: Cannot resolve the name 'soapenc:Array' to a(n) 'type definition' component. line 505 of file:/E:/service1.xml#types?schema2 [ERROR] undefined simple or complex type 'soape

Test is not an annotation type 异常

1. 异常如下: 今天遇到了这个错,也是郁闷了. 提示Test不是一个注释类型: 2. 异常分析: 刚开始以为自己把 Test 拼写错了: 后来以为版本有问题,所以又换成低版本的. 不行!!! 后来才发现,同包下有一个自定义的类,类名为 Test. 3. 解决方案 把同包下名为Test的类改名为Demo,OK.

Annotation Type JsonFormat

最近在调试接口时发现:接口传递过来的jSON数据格式中时间格式是字符串的形式: {"name":"满300送50", "merchantId":"1", "needMoney":"100", "money":"50", "useMoney":"300", "beginDatetime":&

The prefix &quot;p&quot; for attribute &quot;p:message&quot; associated with an element type &quot;bean&quot;

报错的主要原因是因为,引用了spEL表达式,但是却没有引用相应的包 在bean.xml 中添加 xmlns:p="http://www.springframework.org/schema/p" <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=&

Attribute &quot;resultType&quot; must be declared for element type &quot;update&quot; or &quot;insert&quot;

仔细查看错误如图所示: 解决错误就是把resultType去掉,因为在insert和update语句中是没有返回值的.小坑小坑 转自:https://blog.csdn.net/u013144287/article/details/77328265 原文地址:https://www.cnblogs.com/isme-zjh/p/11764209.html

mvc Attribute NotMapped DisplayName Required 等介绍

各个属性添加各自的Attribute,来对属性进行约束:在理想状态下,我们的界面甚至不用手动添加属性的描述性文字 在使用Attribute时,需要引入以下命名空间 using System.ComponentModel; using System.ComponentModel.DataAnnotations; 常用Attribute Required: 指定为必填字段,即指定数据库对应的列不允许为 null 值 StringLength: 指定字段的长度范围,最小长度 NotMapped: 没有