开发者

3 counts of IllegalAnnotationExceptions

开发者 https://www.devze.com 2023-01-08 15:31 出处:网络
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.

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开发者_JS百科 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.


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() {
}


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 .

0

精彩评论

暂无评论...
验证码 换一张
取 消