JAXB Exception: Class not known to this context while marshalling - java

I am getting below error while marshalling. I generated xml's from schemas.
[javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: class org.com.AccountsComplexType nor any of its super class is known to this context.
After a little research, some people says "Try adding #XmlSeeAlso(...class)" but the problem is AccountsComplexType class is not a XmlRootElement.
Is there any idea what am I doing wrong?

If you generated the classes form an XML schema then you should bootstrap the JAXBContext from the package name of the generated model or the ObjectFactory class.

Related

jaxb.index and nested classes (and OSGi)

When I try to refer to nested classes within my jaxb.index file, an exception is thrown during serialization. How can this be avoided?
This is in an Eclipse RCP application. The classes causing the exception are in a different plug-in than the one that creates the JAXB context and initiates serialization. The classes are in one of the plug-in's exported packages.
The class structure looks like this (names have been changed):
#XmlRootElement(name="foo")
#XmlAccessorType (XmlAccessType.FIELD)
public class Foo extends AbstractFoo {
...
#XmlRootElement(name="fooMetric")
#XmlAccessorType (XmlAccessType.FIELD)
public static class FooMetric implements IFooMetric {
...
}
}
The jaxb.index file contains these:
Foo
Foo.FooMetric
During serialization, the exception says to use "OuterClass.InnerClass" -- which I'm doing.
javax.xml.bind.JAXBException: error loading class "Foo.FooMetric" listed in com/mypackage/jaxb.index, make sure that entries are accessable on CLASSPATH and of the form "ClassName" or "OuterClass.InnerClass", not "ClassName.class" or "fully.qualified.ClassName"
- with linked exception:
[java.lang.ClassNotFoundException: com.mypackage.Foo.FooMetric]
The javadocs ("Format for jaxb.index") also suggests that jaxb.index can contain entries of the form OuterClass.InnerClass.
Constraints on class name occuring in a jaxb.index file are:
Must not end with ".class".
Class names are resolved relative to package containing jaxb.index file. Only classes occuring directly in package containing jaxb.index file are allowed.
Fully qualified class names are not allowed. A qualified class name,relative to current package, is only allowed to specify a nested or inner class.
However, this does not appear to work. What will make it work?
The solution I found (by trial and error) was to use OuterClass$InnerClass in jaxb.index instead of OuterClass.InnerClass. This allows serialization to complete successfully.
However, I haven't found any authoritative source that recommends this.
[I'm posting this solution per stackoverflow guidelines, but would love to see and accept a better answer.]

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

How to make my root jaxb generated class extend my own class

I want my xjc generated root class extend one of my class A, which is nothing but a wrapper around the root class. Can i do that without modifying the xsd file (how to force schema compiled classes to extend specific class outside schema)
You can define a super-root class (see http://jaxb.java.net/nonav/2.0.2/docs/vendorCustomizations.html). Every generated class (in that xjc run) will be extending that root class.
The required global binding can be declared in an external binding file instead of inside the xsd (see http://java.sun.com/webservices/docs/1.4/tutorial/doc/JAXBUsing4.html#wp148515). Then you can pass it to the xjc generator together with the xsd.

Marshalling fails with exception about MarshallerImpl not known to this context

I have a relatively simple package of 8 Java classes generated from an XML schema using JAXB XJC. I also have a utility class to marshal and unmarshal instances of the class.
This works
The utility class can successfully unmarshal a valid XML document into an instance of the ‘root’ class WordMergeInfo. For example this works fine:
JAXBContext jc = JAXBContext.newInstance(WordMergeInfo.class);
Unmarshaller um = jc.createUnmarshaller();
return (WordMergeInfo)um.unmarshal(inputStream);
This doesn’t work
But marshalling to a string fails. In this code:
JAXBContext jc = JAXBContext.newInstance(WordMergeInfo.class);
Marshaller m = jc.createMarshaller();
StringWriter writer = new StringWriter();
m.marshal(m, writer);
return writer.toString();
the call to Marshaller.marshal fails with the following error:
javax.xml.bind.JAXBException: class com.sun.xml.bind.v2.runtime.MarshallerImpl nor any of its super class is known to this context.
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:594)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:482)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:315)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:244)
As I understand it, nor any of its super class is known to this context means that a JAXB class needed for marshalling cannot be found. So why can’t one of the JAXB implementation classes be found, when the same class is in the stack trace?
Context
This error showed up in a unit test of my class, run under Maven. The dependencies are:
javax.xml.bind:jaxb-api:2.1
com.sun.xml.bind:jaxb-impl:2.1.13
I got the same error with earlier versions of these (2.0 and 2.0.3, respectively).
The Maven test class path is:
C:\Users\mstra.CUSTMAN\Workspace\DARTCorrModule\xml\target\test-classes
C:\Users\mstra.CUSTMAN\Workspace\DARTCorrModule\xml\target\classes
C:\Users\mstra.CUSTMAN\.m2\repository\javax\xml\bind\jaxb-api\2.1\jaxb-api-2.1.jar
C:\Users\mstra.CUSTMAN\.m2\repository\javax\xml\stream\stax-api\1.0-2\stax-api-1.0-2.jar
C:\Users\mstra.CUSTMAN\.m2\repository\javax\activation\activation\1.1\activation-1.1.jar
C:\Users\mstra.CUSTMAN\.m2\repository\com\sun\xml\bind\jaxb-impl\2.1.13\jaxb-impl-2.1.13.jar
C:\Users\mstra.CUSTMAN\.m2\repository\junit\junit\4.8.2\junit-4.8.2.jar
C:\Users\mstra.CUSTMAN\.m2\repository\org\mockito\mockito-all\1.8.5\mockito-all-1.8.5.jar
C:\Users\mstra.CUSTMAN\.m2\repository\javax\ejb\ejb-api\3.0\ejb-api-3.0.jar
C:\Users\mstra.CUSTMAN\.m2\repository\org\slf4j\slf4j-api\1.6.4\slf4j-api-1.6.4.jar
Any insight is appreciated.
nor any of its super class is known to this context
This means that the class is not registered as a marshallable class in the JAXB context.
Your error is on this line:
m.marshal(m, writer);
You are trying to marshal the marshaller itself. You probably meant to marshal a WordMergeInfo object.

web service - client classes

The web service that I implemented is up and running, when I try to run the client I get the following error with regard to the classes that were generated using wsimport,
Caused by: java.security.PrivilegedActionException: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 4 counts of IllegalAnnotationExceptions
Two classes have the same XML type name "{http://server.agency.hw2/}userJoined". Use #XmlType.name and #XmlType.namespace to assign different names to them.
this problem is related to the following location:
at hw2.chat.backend.main.generatedfromserver.UserJoined
at public javax.xml.bind.JAXBElement hw2.chat.backend.main.generatedfromserver.ObjectFactory.createUserJoined(hw2.chat.backend.main.generatedfromserver.UserJoined)
at hw2.chat.backend.main.generatedfromserver.ObjectFactory
this problem is related to the following location:
at ChatCompany.BackendChatServer.hw2.chat.backend.main.generatedfromserver.UserJoined
Two classes have the same XML type name "{http://server.agency.hw2/}userJoinedResponse". Use #XmlType.name and #XmlType.namespace to assign different names to them.
this problem is related to the following location:
at hw2.chat.backend.main.generatedfromserver.UserJoinedResponse
at public javax.xml.bind.JAXBElement hw2.chat.backend.main.generatedfromserver.ObjectFactory.createUserJoinedResponse(hw2.chat.backend.main.generatedfromserver.UserJoinedResponse)
But I can't figure out what exactly is meant by the error. I am assuming I need to change something in annotations in these classes as pointed out by the compiler:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "userJoinedResponse")
public class UserJoinedResponse {
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "userJoined", propOrder = {
"arg0"
})
public class UserJoined {
could someone please point out why there's a name collision and what annotations I need to change?
thanks
My teacher has suggested (among other reasons) that maybe there is something wrong with the way i ran the wsimport utility, so I regenerated the client class files using wsimport and the problem was solved. it might have been that I didn't specify the package name at the beginning and did it manually first time but I am uncertain.
I got the same exception. The reason is a similarity of names both classes. When you have class with name UserJoined during wsdl generation process JAXB generate element with name UserJoinedResponse (see your wsdl file).
And the you try to add another class with the same name (UserJoinedResponse). So just rename UserJoinedResponse class or annotate it as mentioned in the exception.

Categories

Resources