解组jaxb对象时出错

时间:2012-10-19 16:23:02

标签: java jaxb2 unmarshalling

我有针对XML架构的jaxb对象。该xml架构可以具有不同的根元素。哪个在主项目中工作得很好。基于这些生成的对象,我试着写一些测试用例。

我编写了以下代码来获取JAXBContext

JAXBContext jaxbContext = JAXBContext.newInstance("com.store.web.ws.v1.model");

我得到以下异常

com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
java.lang.StackTraceElement does not have a no-arg default constructor.
    this problem is related to the following location:
        at java.lang.StackTraceElement
        at public java.lang.StackTraceElement[] java.lang.Throwable.getStackTrace()
        at java.lang.Throwable
        at java.lang.Error
        at public java.lang.Error com.store.web.ws.v1.model.ObjectFactory.createError()
        at com.store.web.ws.v1.model.ObjectFactory

    at com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:106)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:466)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:298)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:141)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1157)
    at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:145)
    at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:236)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:128)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:277)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:372)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:337)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:244)
    at com.store.web.ws.v1.resource.RequestInfo.main(RequestInfo.java:171)

然而,我将上下文行更改为

的那一刻
JAXBContext jaxbContext = JAXBContext.newInstance(BookType.class);

我得到以下异常SEVERE: unexpected element (uri:"", local:"book"). Expected elements are (none),这可以基于我之前的question

来理解

有人可以帮助我吗?我想基于包名创建一个上下文。

1 个答案:

答案 0 :(得分:3)

看起来,从您的模式生成的类之一被称为Error,并且编译器已选择java.lang.Error类上的com.store.web.ws.v1.model.Error类。尝试明确

import com.store.web.ws.v1.model.Error;

位于ObjectFactory.java的顶部并重新编译,或者无法编辑代码以使用完全限定的类名。

public com.store.web.ws.v1.Error createError() {
  return new com.store.web.ws.v1.Error();
}

(或更改类名并使用@XmlRootElement(name="Error")重新映射到正确的元素名称)