Search code examples
javajaxb2unmarshalling

Error unmarshalling jaxb objects


I have jaxb objects against an XML schema. That xml schema can have different root element. Which is working perfectly fine in the main project. Based on those generated objects, I am trying to write some test cases.

I wrote following code to get JAXBContext

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

and i get following exception

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)

However, the moment i change my context line to

JAXBContext jaxbContext = JAXBContext.newInstance(BookType.class);

I get following exception SEVERE: unexpected element (uri:"", local:"book"). Expected elements are (none) which is understandable based on my earlier question

Can someone help me here ? I want to create a context based on package name.


Solution

  • That looks like one of the classes generated from your schema is called Error, and the compiler has picked the java.lang.Error class over the com.store.web.ws.v1.model.Error class. Try putting an explicit

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

    at the top of ObjectFactory.java and recompiling it, or failing that edit the code to use fully-qualified class names.

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

    (or change the class name and remap it to the right element name using @XmlRootElement(name="Error"))