2

我有针对 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)根据我之前的问题,我得到以下异常是可以理解的

有人可以在这里帮助我吗?我想根据包名称创建一个上下文。

4

1 回答 1

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

于 2012-10-19T16:31:30.053 回答