3 次 IllegalAnnotationExceptions 计数

发布于 2024-09-10 12:13:18 字数 1774 浏览 1 评论 0原文

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

我的结构如下:

A 是 TestCase 基类。

B 扩展了 A

C 扩展了 B

基类 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;
  }
}

类 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;
   }       
}

类 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;
    }
}

为了解组我的 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;
 }

我的 xml 文件,如下所示:

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

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

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.

My structure is like:

A is the base TestCase class.

B extends A.

C extends B.

Base Class 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:

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:

@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

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:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

今天小雨转甜 2024-09-17 12:13:18

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

添加到 A:

public A() {
}

添加到 B:

public B() {
}

并添加到 C:

public C() {
}

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

Add to A:

public A() {
}

Add to B:

public B() {
}

And add to C:

public C() {
}
娇纵 2024-09-17 12:13:18

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

示例:

@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)

}

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

就像你有 3 个地名不匹配一样。因此有 3 个 IllegalAnnotationExceptions 计数。

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.

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文