3 次 IllegalAnnotationExceptions 计数
我以前从未使用过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IllegalAnnotationExceptions 是因为您在 A、B 和 C 上没有默认的零参数构造函数。
添加到 A:
添加到 B:
并添加到 C:
The IllegalAnnotationExceptions are due to you not having default zero-arg constructors on A, B, and C.
Add to A:
Add to B:
And add to C:
这是因为您正在创建 JAXBcontext 实例的该类的子元素与其中定义的元素名称不同名。
示例:
在上面的类中,您没有 "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:
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 .