java 3 次 IllegalAnnotationExceptions

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3292483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:10:49  来源:igfitidea点击:

3 counts of IllegalAnnotationExceptions

javajaxbunmarshalling

提问by Sami

I have never used JAXB before. I am working on a test harnesses project. I have around 20 different testcases. When I run my test, I get this error.

我以前从未使用过 JAXB。我正在研究一个测试工具项目。我有大约 20 个不同的测试用例。当我运行测试时,出现此错误。

My structure is like:

我的结构是这样的:

Ais the base TestCase class.

A是基本的 TestCase 类。

Bextends A.

B延伸A

Cextends B.

C延伸B

Base Class A:

基类A:

public class A {
  public A(String t){
     testName = t;
  }

  private String aData;
  private String testName;
  public void setAData(String a){
     aData = a;
  }

  public void getAData(){
     return aData;
  }

  public void setTestName(String t){
     testName = t;
  }

  public void getTestName(){
     return testName;
  }
}

Class B:

B类:

public class B extends A{ 
   public B(String testName){
      super(testName);
   }

   private String bData;
   public void setBData(String b){
      bData = b.trim();
   }
   public String getData(){
      return bData;
   }       
}

Class C:

C类:

@XmlRootElement(name="C")
public class C extends B{
    public C(String testName){
        super(testName);
    }

    private String cData;
    public void setCData(String c){
        cData = c;
    }
    public String getCData(){
        return cData;
    }
}

and for unmarshalling my xml files i wrote

为了解组我写的 xml 文件

public C unmarshall(C test, String dir){
    try {
        JAXBContext jc = JAXBContext.newInstance(c.getClass);
        Unmarshaller u = jc.createUnmarshaller();

        test = (C)u.unmarshal(new FileInputStream(dir));
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    return test;
 }

my xml file looks like:

我的 xml 文件看起来像:

<C>
   <aData> aaaa </aData>
   <bData> bbbb </bData>
   <cData> cccc </cData>
</C>

when i run my code i get 3 counts of IllegalAnnotationException.

当我运行我的代码时,我得到了 3 个 IllegalAnnotationException 计数。

回答by bdoughan

The IllegalAnnotationExceptions are due to you not having default zero-arg constructors on A, B, and C.

IllegalAnnotationExceptions 是由于您在 A、B 和 C 上没有默认的零参数构造函数。

Add to A:

添加到A:

public A() {
}

Add to B:

添加到B:

public B() {
}

And add to C:

并添加到 C:

public C() {
}

回答by BinDev

This is because the sub-elements of that class you are creating JAXBcontext instance ,doesn't have the same name as of the element names defined inside it.

这是因为您正在创建 JAXBcontext 实例的该类的子元素与其中定义的元素名称不具有相同的名称。

Example:

例子:

@XmlType(name = "xyz", propOrder =
{ "a", "b", "c", "d" })
@XmlRootElement(name = "testClass")
public class TestClass
{

  @XmlElement(required = true)
  protected Status status;
  @XmlElement(required = true)
  protected String mno;
  @XmlElement(required = true)

}

In the above class you don't have "xyz" , but if you will put the property name that is not available JAXBContext instantiation throws IlligalAnnotationException.

在上面的类中,您没有 "xyz" ,但是如果您将放置不可用的属性名称,则 JAXBContext 实例化会抛出 IlligalAnnotationException。

Like that you have 3 places name mismatch. So 3 counts of IllegalAnnotationExceptions .

就像你有 3 个地名不匹配。所以 3 个 IllegalAnnotationExceptions 计数。