This exception occur when you are using JAXB to marshal a java object (collection type) to xml format. The stack trace looks like this:
Exception in thread "main" javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context. at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source) at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source) at com.howtodoinjava.jaxb.examples.list.TestEmployeeMarshing.main(TestEmployeeMarshing.java:58)
Reason
The above exception occurs because JAXB always expects a @XmlRootElement annotation on the entity, it gets to marshal. This is mandatory and can not be skipped. This @XmlRootElement annotation is required to get meta data from root element of XML marshalled from java object.
ArrayList class OR any java collection class does not have any JAXB annotations on it. Due to this JAXB is unable to parse any such java objects and raises this error.
Solution : Create wrapper class
This is recommended approach because it gives you flexibility to add/remove fields in future e.g. size attribute.
@XmlRootElement(name = "employees") @XmlAccessorType (XmlAccessType.FIELD) public class Employees { @XmlElement(name = "employee") private List<Employee> employees = null; public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } }
Now you can use this class as below:
static Employees employees = new Employees(); static { employees.setEmployees(new ArrayList<Employee>()); Employee emp1 = new Employee(); emp1.setId(1); emp1.setFirstName("Lokesh"); emp1.setLastName("Gupta"); emp1.setIncome(100.0); Employee emp2 = new Employee(); emp2.setId(2); emp2.setFirstName("John"); emp2.setLastName("Mclane"); emp2.setIncome(200.0); employees.getEmployees().add(emp1); employees.getEmployees().add(emp2); } private static void marshalingExample() throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(Employees.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(employees, System.out); } Output: <employees> <employee> <id>1</id> <firstName>Lokesh</firstName> <lastName>Gupta</lastName> <income>100.0</income> </employee> <employee> <id>2</id> <firstName>John</firstName> <lastName>Mclane</lastName> <income>200.0</income> </employee> </employees>
See the complete example in this post: Marshalling example java ArrayList or Set
Happy Learning !!
Please note that I am uncertain of the «Found in version» field
above. I’m using Glassfish v1 UR1 P01, so please correct my selection
if I’m incorrect.
I am also uncertain about the subcomponent field. wsimport was my
best guess.
I have written an object called NanonationDictionary that is roughly
a Map, intending this object to be a return type from web service operations.
(I have done this to work around the normal HashMap not working.) My
NanonationDictionary class seems to work fine.
Sometimes I also want to return a List of NanonationDictionary, and this can
also work just fine.
However, if I have a service with an operation that returns List (and it
contains a NanonationDictionary) then I must have some other operation
in that service class that returns a NanonationDictionary.
If no such other operation exists, I get the following exception at runtime,
when the service operation is invoked:
wsimport successful
DPL5306:Servlet Web Service Endpoint [ServiceTwo] listening at address
http://12.21.10.66:8080/JAXWSTestCase/ServiceTwoService
Cannot log SOAP Message Error in encoding SOAP Message
Error in encoding SOAP Message
at
com.sun.xml.ws.encoding.soap.server.SOAPXMLEncoder.toSOAPMessage(SOAPXMLEncoder.java:115)
at
com.sun.xml.ws.protocol.soap.server.SOAPMessageDispatcher.makeSOAPMessage(SOAPMessageDispatcher.java:352)
at
com.sun.xml.ws.protocol.soap.server.SOAPMessageDispatcher.receive(SOAPMessageDispatcher.java:157)
at com.sun.xml.ws.server.Tie.handle(Tie.java:90)
at com.sun.enterprise.webservice.JAXWSServlet.doPost(JAXWSServlet.java:195)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at
org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:397)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:278)
at
org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:566)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:536)
at
org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:240)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:179)
at
org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:566)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:73)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:182)
at
org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:566)
at
com.sun.enterprise.web.VirtualServerPipeline.invoke(VirtualServerPipeline.java:120)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:939)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:137)
at
org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:566)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:536)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:939)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:239)
at
com.sun.enterprise.web.connector.grizzly.ProcessorTask.invokeAdapter(ProcessorTask.java:667)
at
com.sun.enterprise.web.connector.grizzly.ProcessorTask.processNonBlocked(ProcessorTask.java:574)
at
com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:844)
at
com.sun.enterprise.web.connector.grizzly.ReadTask.executeProcessorTask(ReadTask.java:287)
at
com.sun.enterprise.web.connector.grizzly.ReadTask.doTask(ReadTask.java:212)
at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:252)
at
com.sun.enterprise.web.connector.grizzly.WorkerThread.run(WorkerThread.java:75)
Caused by: javax.xml.bind.MarshalException
- with linked exception:
[javax.xml.bind.JAXBException: net.nanonation.util.NanonationDictionary nor any
of its super class is known to this context]
at
com.sun.xml.ws.encoding.jaxb.JAXBBridgeInfo.serialize(JAXBBridgeInfo.java:90)
at
com.sun.xml.ws.encoding.soap.SOAPEncoder.writeJAXBBridgeInfo(SOAPEncoder.java:254)
at com.sun.xml.ws.encoding.soap.SOAPEncoder.writeBody(SOAPEncoder.java:571)
at
com.sun.xml.ws.encoding.soap.server.SOAPXMLEncoder.toSOAPMessage(SOAPXMLEncoder.java:97)
… 30 more
Caused by: javax.xml.bind.MarshalException - with linked exception:
[javax.xml.bind.JAXBException: net.nanonation.util.NanonationDictionary nor any
of its super class is known to this context]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:246)
at com.sun.xml.bind.v2.runtime.BridgeImpl.marshal(BridgeImpl.java:64)
at com.sun.xml.bind.api.Bridge.marshal(Bridge.java:102)
at
com.sun.xml.ws.encoding.jaxb.JAXBBridgeInfo.serialize(JAXBBridgeInfo.java:88)
… 33 more
Caused by: javax.xml.bind.JAXBException:
net.nanonation.util.NanonationDictionary nor any of its super class is known to
this context
at
com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:223)
at
com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:238)
at
com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:587)
at
com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty.java:29)
at
com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:132)
at
com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:101)
at
com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:286)
at
com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:619)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:241)
… 36 more
Caused by: javax.xml.bind.JAXBException:
net.nanonation.util.NanonationDictionary nor any of its super class is known to
this context
at
com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:477)
at
com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:582)
… 42 more
Error in encoding SOAP Message
Error in encoding SOAP Message
at
com.sun.xml.ws.encoding.soap.server.SOAPXMLEncoder.toSOAPMessage(SOAPXMLEncoder.java:115)
at
com.sun.xml.ws.protocol.soap.server.SOAPMessageDispatcher.makeSOAPMessage(SOAPMessageDispatcher.java:352)
at
com.sun.xml.ws.protocol.soap.server.SOAPMessageDispatcher.receive(SOAPMessageDispatcher.java:157)
at com.sun.xml.ws.server.Tie.handle(Tie.java:90)
at com.sun.enterprise.webservice.JAXWSServlet.doPost(JAXWSServlet.java:195)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at
org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:397)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:278)
at
org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:566)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:536)
at
org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:240)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:179)
at
org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:566)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:73)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:182)
at
org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:566)
at
com.sun.enterprise.web.VirtualServerPipeline.invoke(VirtualServerPipeline.java:120)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:939)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:137)
at
org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:566)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:536)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:939)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:239)
at
com.sun.enterprise.web.connector.grizzly.ProcessorTask.invokeAdapter(ProcessorTask.java:667)
at
com.sun.enterprise.web.connector.grizzly.ProcessorTask.processNonBlocked(ProcessorTask.java:574)
at
com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:844)
at
com.sun.enterprise.web.connector.grizzly.ReadTask.executeProcessorTask(ReadTask.java:287)
at
com.sun.enterprise.web.connector.grizzly.ReadTask.doTask(ReadTask.java:212)
at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:252)
at
com.sun.enterprise.web.connector.grizzly.WorkerThread.run(WorkerThread.java:75)
Caused by: javax.xml.bind.MarshalException - with linked exception:
[javax.xml.bind.JAXBException: net.nanonation.util.NanonationDictionary nor any
of its super class is known to this context]
at
com.sun.xml.ws.encoding.jaxb.JAXBBridgeInfo.serialize(JAXBBridgeInfo.java:90)
at
com.sun.xml.ws.encoding.soap.SOAPEncoder.writeJAXBBridgeInfo(SOAPEncoder.java:254)
at com.sun.xml.ws.encoding.soap.SOAPEncoder.writeBody(SOAPEncoder.java:571)
at
com.sun.xml.ws.encoding.soap.server.SOAPXMLEncoder.toSOAPMessage(SOAPXMLEncoder.java:97)
… 30 more
Caused by: javax.xml.bind.MarshalException - with linked exception:
[javax.xml.bind.JAXBException: net.nanonation.util.NanonationDictionary nor any
of its super class is known to this context]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:246)
at com.sun.xml.bind.v2.runtime.BridgeImpl.marshal(BridgeImpl.java:64)
at com.sun.xml.bind.api.Bridge.marshal(Bridge.java:102)
at
com.sun.xml.ws.encoding.jaxb.JAXBBridgeInfo.serialize(JAXBBridgeInfo.java:88)
… 33 more
Caused by: javax.xml.bind.JAXBException:
net.nanonation.util.NanonationDictionary nor any of its super class is known to
this context
at
com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:223)
at
com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:238)
at
com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:587)
at
com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty.java:29)
at
com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:132)
at
com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:101)
at
com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:286)
at
com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:619)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:241)
… 36 more
Caused by: javax.xml.bind.JAXBException:
net.nanonation.util.NanonationDictionary nor any of its super class is known to
this context
at
com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:477)
at
com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:582)
… 42 more
Error in encoding SOAP Message
at
com.sun.xml.ws.encoding.soap.server.SOAPXMLEncoder.toSOAPMessage(SOAPXMLEncoder.java:115)
at
com.sun.xml.ws.protocol.soap.server.SOAPMessageDispatcher.makeSOAPMessage(SOAPMessageDispatcher.java:352)
at
com.sun.xml.ws.protocol.soap.server.SOAPMessageDispatcher.receive(SOAPMessageDispatcher.java:157)
at com.sun.xml.ws.server.Tie.handle(Tie.java:90)
at com.sun.enterprise.webservice.JAXWSServlet.doPost(JAXWSServlet.java:195)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at
org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:397)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:278)
at
org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:566)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:536)
at
org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:240)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:179)
at
org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:566)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:73)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:182)
at
org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:566)
at
com.sun.enterprise.web.VirtualServerPipeline.invoke(VirtualServerPipeline.java:120)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:939)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:137)
at
org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:566)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:536)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:939)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:239)
at
com.sun.enterprise.web.connector.grizzly.ProcessorTask.invokeAdapter(ProcessorTask.java:667)
at
com.sun.enterprise.web.connector.grizzly.ProcessorTask.processNonBlocked(ProcessorTask.java:574)
at
com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:844)
at
com.sun.enterprise.web.connector.grizzly.ReadTask.executeProcessorTask(ReadTask.java:287)
at
com.sun.enterprise.web.connector.grizzly.ReadTask.doTask(ReadTask.java:212)
at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:252)
at
com.sun.enterprise.web.connector.grizzly.WorkerThread.run(WorkerThread.java:75)
Caused by: javax.xml.bind.MarshalException - with linked exception:
[javax.xml.bind.JAXBException: net.nanonation.util.NanonationDictionary nor any
of its super class is known to this context]
at
com.sun.xml.ws.encoding.jaxb.JAXBBridgeInfo.serialize(JAXBBridgeInfo.java:90)
at
com.sun.xml.ws.encoding.soap.SOAPEncoder.writeJAXBBridgeInfo(SOAPEncoder.java:254)
at com.sun.xml.ws.encoding.soap.SOAPEncoder.writeBody(SOAPEncoder.java:571)
at
com.sun.xml.ws.encoding.soap.server.SOAPXMLEncoder.toSOAPMessage(SOAPXMLEncoder.java:97)
… 30 more
Caused by: javax.xml.bind.MarshalException - with linked exception:
[javax.xml.bind.JAXBException: net.nanonation.util.NanonationDictionary nor any
of its super class is known to this context]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:246)
at com.sun.xml.bind.v2.runtime.BridgeImpl.marshal(BridgeImpl.java:64)
at com.sun.xml.bind.api.Bridge.marshal(Bridge.java:102)
at
com.sun.xml.ws.encoding.jaxb.JAXBBridgeInfo.serialize(JAXBBridgeInfo.java:88)
… 33 more
Caused by: javax.xml.bind.JAXBException:
net.nanonation.util.NanonationDictionary nor any of its super class is known to
this context
at
com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:223)
at
com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:238)
at
com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:587)
at
com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty.java:29)
at
com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:132)
at
com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:101)
at
com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:286)
at
com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:619)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:241)
… 36 more
Caused by: javax.xml.bind.JAXBException:
net.nanonation.util.NanonationDictionary nor any of its super class is known to
this context
at
com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:477)
at
com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:582)
… 42 more
Environment
Operating System: Mac OS X
Platform: Macintosh
Affected Versions
[2.0 FCS]
Answer by Kohen Mayer
This error message happens either because your ProfileDto class is not registered in the JAXB Content, or the class using it does not use @XmlSeeAlso(ProfileDto.class) to make processable by JAXB.,I had this error because I registered the wrong class in this line of code:,JAXBException occurred : class
com.octory.ws.dto.ProfileDto nor any
of its super class is known to this
context…,Fixed it by setting the class name to the property «classesToBeBound» of the JAXB marshaller:
I had this error because I registered the wrong class in this line of code:
JAXBContext context = JAXBContext.newInstance(MyRootXmlClass.class);
Answer by Preston Savage
ArrayList class OR any java collection class does not have any JAXB annotations on it. Due to this JAXB is unable to parse any such java objects and raises this error.,JAXB – IllegalAnnotationExceptions,This exception occur when you are using JAXB to marshal a java object (collection type) to xml format. The stack trace looks like this:,See the complete example in this post: Marshalling example java ArrayList or Set
This exception occur when you are using JAXB to marshal a java object (collection type) to xml format. The stack trace looks like this:
Exception in thread "main" javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context.
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
at com.howtodoinjava.jaxb.examples.list.TestEmployeeMarshing.main(TestEmployeeMarshing.java:58)
This is recommended approach because it gives you flexibility to add/remove fields in future e.g. size attribute.
@XmlRootElement(name = "employees")
@XmlAccessorType (XmlAccessType.FIELD)
public class Employees
{
@XmlElement(name = "employee")
private List<Employee> employees = null;
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
Now you can use this class as below:
static Employees employees = new Employees();
static
{
employees.setEmployees(new ArrayList<Employee>());
Employee emp1 = new Employee();
emp1.setId(1);
emp1.setFirstName("Lokesh");
emp1.setLastName("Gupta");
emp1.setIncome(100.0);
Employee emp2 = new Employee();
emp2.setId(2);
emp2.setFirstName("John");
emp2.setLastName("Mclane");
emp2.setIncome(200.0);
employees.getEmployees().add(emp1);
employees.getEmployees().add(emp2);
}
private static void marshalingExample() throws JAXBException
{
JAXBContext jaxbContext = JAXBContext.newInstance(Employees.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(employees, System.out);
}
Output:
<employees>
<employee>
<id>1</id>
<firstName>Lokesh</firstName>
<lastName>Gupta</lastName>
<income>100.0</income>
</employee>
<employee>
<id>2</id>
<firstName>John</firstName>
<lastName>Mclane</lastName>
<income>200.0</income>
</employee>
</employees>
Answer by Emilia Weiss
You can track all active APARs for this component.,Search results are not available at this time. Please try again later or use one of the other support options on this page.
When trying to invoke a JAX-WS web service, this exception
might occur:
[9/30/08 7:57:45:468 PDT] 00000020 ExceptionFact 1
org.apache.axis2.jaxws.ExceptionFactory logRootCause
stack:javax.xml.bind.JAXBException:
com.somecom.lib.business.xml.ComIdentifierXml is not known to
this context
at
com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBCont
extImpl.java:538)
at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.getBea
nInfo(UnmarshallerImpl.java:519)
at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmars
hal(UnmarshallerImpl.java:319)
at
org.apache.axis2.jaxws.message.databinding.impl.JAXBBlockImpl$4.
run(JAXBBlockImpl.java:513)
at
...
Answer by Teagan O’Donnell
Throwable cause;new JAXBException(cause),String property;new JAXBException(Messages.format(property)),Common ways to obtain JAXBException,String str;Throwable pLinkedException;new JAXBException(str, pLinkedException)
private void myMethod () { JAXBException j =Throwable cause;new JAXBException(cause)String str;Throwable pLinkedException;new JAXBException(str, pLinkedException)String property;new JAXBException(Messages.format(property))Smart code suggestions by Tabnine}
Answer by Pedro Beasley
As of spring-projects/[email protected], the JAXB annotations have been removed and should no longer cause this issue,I have the same «jaxbexception», and thank @Lance-Drane ,who solves it.,You can access the data through a browser as well as CURL if you change all GetMapping annotations to something like this:,Sidenote: My IDE (IntelliJ) also wants to add imports for linkTo and methodOn, while I cannot find imports for these in rest/src/main/java/payroll/EmployeeController.java.
Whitelabel Error PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Oct 19 18:23:16 CEST 2018There was an unexpected error (type=Internal Server Error, status=500).
Could not marshal [Resources { content: [Resource { content: Employee(id=1, name=Bilbo Baggins, role=burglar), links: [<http://localhost:8080/employees/1>;rel="self", <http://localhost:8080/employees>;rel="employees"] },
Resource { content: Employee(id=2, name=Frodo Baggins, role=thief), links: [<http://localhost:8080/employees/2>;rel="self", <http://localhost:8080/employees>;rel="employees"] }], links: [<http://localhost:8080/employees>;rel="self"] }]: null;
nested exception is javax.xml.bind.MarshalException - with linked exception: [com.sun.istack.internal.SAXException2: unable to marshal type "org.springframework.hateoas.Resource" as an element because it is not known to this context.]
I am getting above exception, looking for solution, any help will be much appreciated. Found the same issue in some other messages, but they were not working for me. Please see the below code.
javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.SAXException2: class com.mycompany.soma.ws.rest.v1.model.test.EmployeeConstruction nor any of its super class is known to this context.
javax.xml.bind.JAXBException: class com.mycompany.soma.ws.rest.v1.model.test.EmployeeConstruction nor any of its super class is known to this context.]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:326)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:251)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
at com.mycompany.soma.ws.rest.v1.model.test.Test.main(Test.java:39)
.
<snapshots>
<employees>
<employee>
<name>Dan</name>
</employee>
<employee>
<name>Samy</name>
</employee>
</employees>
</shapshots>
.
package com.mycompany.soma.ws.rest.v1.model.test;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class Test {
public static void main(String[] args) {
EmployeeConstruction ec = new EmployeeConstruction();
ec.setName("Construction employee");
ec.setSomeContrction("construction bulding");
EmployeesElement<EmployeeConstruction> ee = new EmployeesElement<EmployeeConstruction>();
List<EmployeeConstruction> list = new ArrayList<EmployeeConstruction>();
list.add(ec);
ee.setEmployees(list);
Snapshots<EmployeeConstruction> snapshots = new Snapshots<EmployeeConstruction>();
snapshots.setEmployeesElement(ee);
JAXBContext jaxbContext;
try {
jaxbContext = JAXBContext.newInstance(Snapshots.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(snapshots, System.out);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.mycompany.soma.ws.rest.v1.model.test;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"employeesElement"})
@XmlRootElement(name = "snapshots")
public class Snapshots<T> {
@XmlElement(name = "employees")
private EmployeesElement<T> employeesElement;
public EmployeesElement<T> getEmployeesElement() {
return employeesElement;
}
public void setEmployeesElement(EmployeesElement<T> employeesElement) {
this.employeesElement = employeesElement;
}
}
package com.mycompany.soma.ws.rest.v1.model.test;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "employees")
public class EmployeesElement<T> {
@XmlElement(name = "employees", nillable = true, required = false)
List<T> employees;
public List<T> getEmployees() {
return employees;
}
public void setEmployees(List<T> employees) {
this.employees = employees;
}
}
package com.mycompany.soma.ws.rest.v1.model.test;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "wsemployee")
@XmlType(name = "", propOrder = {"name"})
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({EmployeeConstruction.class, EmployeeManager.class})
public class Employee {
@XmlElement(required = true)
protected String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.mycompany.soma.ws.rest.v1.model.test;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlSeeAlso;
@XmlRootElement(name = "employee")
@XmlType(name = "", propOrder = {"someContrction"})
@XmlAccessorType(XmlAccessType.FIELD)
public class EmployeeConstruction extends Employee {
@XmlElement(required = true)
protected String someContrction;
public String getSomeContrction() {
return someContrction;
}
public void setSomeContrction(String someContrction) {
this.someContrction = someContrction;
}
}
package com.mycompany.soma.ws.rest.v1.model.test;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "employee")
@XmlType(name = "", propOrder = {"someManaging"})
@XmlAccessorType(XmlAccessType.FIELD)
public class EmployeeManager extends Employee {
@XmlElement(required = true)
protected String someManaging;
public String getSomeManaging() {
return someManaging;
}
public void setSomeManaging(String someManaging) {
this.someManaging = someManaging;
}
}
You can pass several classes in newInstance
jaxbContext = JAXBContext.newInstance(Snapshots.class, EmployeeConstruction.class);
(Java version 1.6.0_24 and JBoss AS 6.0.0.Final (applies also to JBoss 5.1))
Trying to marshall an Java object graph to XML in a servlet I get the following apparently well known exception:
22:20:10,140 ERROR [STDERR] javax.xml.bind.MarshalException
22:20:10,140 ERROR [STDERR] — with linked exception:
22:20:10,140 ERROR [STDERR] [javax.xml.bind.JAXBException: class YY.XXX.printservice.domain.EmployeeDetails nor any of its super class is known to this context.]
(Note: I renamed the package name.)
When I try google for this problem I get hits galore.
But with the solutions and hints offered — for example the use of @XmlSeeAlso — I still wasn’t able to solve the problem…
The strange thing about this is also that it works when the marshalling is done by a standalone client.
These are the two value classes involved:
1. class Report:
{code}
@XmlRootElement(name=»report»)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso(EmployeeDetails.class)
public class Report implements Serializable {
@XmlElement(name=»employee», type=YY.XXXX.printservice.domain.EmployeeDetails.class)
private List<EmployeeDetails> employeeDetails;
public Report(){
}
public Report(List<EmployeeDetails> employeeDetails) {
this.employeeDetails = employeeDetails;
}
public List<EmployeeDetails> getEmployeeDetails() {
return employeeDetails;
}
public void setEmployeeDetails(List<EmployeeDetails> employeeDetails) {
this.employeeDetails = employeeDetails;
}
}
{code}
2. class EmployeeDetails:
{code}
@XmlType(
propOrder = {
«employeeId»
, «firstName»
, «lastName»
, «departmentName»
, «jobTitle»
, «salary»
}
)
@XmlAccessorType(XmlAccessType.FIELD)
public class EmployeeDetails implements Serializable {
private Long employeeId;
private String firstName;
private String lastName;
private String departmentName;
private String jobTitle;
private Float salary;
public EmployeeDetails(){
}
public EmployeeDetails(Long employeeId
, String firstName
, String lastName
, String departmentName
, String jobTitle
, Float salary) {
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
this.departmentName = departmentName;
this.jobTitle = jobTitle;
this.salary = salary;
}
public Long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(Long employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
public Float getSalary() {
return salary;
}
public void setSalary(Float salary) {
this.salary = salary;
}
}
{code}
The relevant parts of the servlet:
{code}
public final class PrintServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB(mappedName=»EmployeeBean/local»)
private EmployeeLocal employeeBean;
private static JAXBContext jaxbContext = null;
private FopFactory fopFactory = FopFactory.newInstance();
private TransformerFactory transformerFactory = TransformerFactory.newInstance();
private DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
public PrintServlet() {
super();
}
public void init() throws ServletException {
try {
jaxbContext = JAXBContext.newInstance( new Class[] {Report.class});
}
catch (JAXBException exception) {
exception.printStackTrace();
throw new ServletException( exception.fillInStackTrace() );
}
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String content = «application/x-java-serialized-object»;
response.setContentType(content);
try {
List<EmployeeDetails> employeeDetailsList = employeeBean.getDetailsForAllEmployees();
Report report = new Report();
report.setEmployeeDetails(employeeDetailsList);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
jaxbContext.createMarshaller().marshal(report, document);
….
{code}
Calling toString on the JAXBContext instance in the servlet shows this:
22:20:09,656 INFO [STDOUT] vfs:/D:/server/jboss-6.0.0.Final/lib/jaxb-impl.jar/com/sun/xml/bind/v2/runtime/JAXBContextImpl.class Build-Id: 2.2
22:20:09,656 INFO [STDOUT] Classes known to this context:
22:20:09,656 INFO [STDOUT] [B
22:20:09,656 INFO [STDOUT] boolean
22:20:09,656 INFO [STDOUT] byte
22:20:09,656 INFO [STDOUT] char
22:20:09,656 INFO [STDOUT] com.sun.xml.bind.api.CompositeStructure
22:20:09,656 INFO [STDOUT] YY.XXX.printservice.domain.EmployeeDetails
22:20:09,656 INFO [STDOUT] YY.XXX.printservice.domain.Report
22:20:09,656 INFO [STDOUT] double
22:20:09,656 INFO [STDOUT] float
22:20:09,656 INFO [STDOUT] int
22:20:09,656 INFO [STDOUT] java.awt.Image
22:20:09,656 INFO [STDOUT] java.io.File
22:20:09,656 INFO [STDOUT] java.lang.Boolean
22:20:09,656 INFO [STDOUT] java.lang.Byte
22:20:09,656 INFO [STDOUT] java.lang.Character
22:20:09,656 INFO [STDOUT] java.lang.Class
22:20:09,656 INFO [STDOUT] java.lang.Double
22:20:09,656 INFO [STDOUT] java.lang.Float
22:20:09,656 INFO [STDOUT] java.lang.Integer
22:20:09,656 INFO [STDOUT] java.lang.Long
22:20:09,656 INFO [STDOUT] java.lang.Object
22:20:09,656 INFO [STDOUT] java.lang.Short
22:20:09,656 INFO [STDOUT] java.lang.String
22:20:09,656 INFO [STDOUT] java.lang.Void
22:20:09,656 INFO [STDOUT] java.math.BigDecimal
22:20:09,656 INFO [STDOUT] java.math.BigInteger
22:20:09,656 INFO [STDOUT] java.net.URI
22:20:09,656 INFO [STDOUT] java.net.URL
22:20:09,656 INFO [STDOUT] java.util.Calendar
22:20:09,656 INFO [STDOUT] java.util.Date
22:20:09,656 INFO [STDOUT] java.util.GregorianCalendar
22:20:09,656 INFO [STDOUT] java.util.UUID
22:20:09,656 INFO [STDOUT] javax.activation.DataHandler
22:20:09,656 INFO [STDOUT] javax.xml.bind.JAXBElement
22:20:09,656 INFO [STDOUT] javax.xml.datatype.Duration
22:20:09,656 INFO [STDOUT] javax.xml.datatype.XMLGregorianCalendar
22:20:09,656 INFO [STDOUT] javax.xml.namespace.QName
22:20:09,656 INFO [STDOUT] javax.xml.transform.Source
22:20:09,656 INFO [STDOUT] long
22:20:09,656 INFO [STDOUT] short
22:20:09,656 INFO [STDOUT] void
Calling toString on the JAXBContext instance of the standalone client shows this:
[java] jar:file:/D:/server/jboss-6.0.0.Final/client/jaxb-impl.jar!/com/sun/xml/bind/v2/runtime/JAXBContextImpl.class Build-Id: 2.2
[java] Classes known to this context:
[java] [B
[java] boolean
[java] byte
[java] char
[java] com.sun.xml.bind.api.CompositeStructure
[java] YY.XXX.printservice.domain.EmployeeDetails
[java] YY.XXX.printservice.domain.Report
[java] double
[java] float
[java] int
[java] java.awt.Image
[java] java.io.File
[java] java.lang.Boolean
[java] java.lang.Byte
[java] java.lang.Character
[java] java.lang.Class
[java] java.lang.Double
[java] java.lang.Float
[java] java.lang.Integer
[java] java.lang.Long
[java] java.lang.Object
[java] java.lang.Short
[java] java.lang.String
[java] java.lang.Void
[java] java.math.BigDecimal
[java] java.math.BigInteger
[java] java.net.URI
[java] java.net.URL
[java] java.util.Calendar
[java] java.util.Date
[java] java.util.GregorianCalendar
[java] java.util.UUID
[java] javax.activation.DataHandler
[java] javax.xml.bind.JAXBElement
[java] javax.xml.datatype.Duration
[java] javax.xml.datatype.XMLGregorianCalendar
[java] javax.xml.namespace.QName
[java] javax.xml.transform.Source
[java] long
[java] short
[java] void
Thus the two JAXBContext instances do not only know the same data types, but they also know the two relevant custom classes Report and EmployeeDetails.
But the similarity continues: Outputting the schema known to both JAXBContext instances shows that they are also the same.
1. server side:
22:20:09,859 INFO [STDOUT] <?xml version=»1.0″ encoding=»UTF-8″ standalone=»yes»?>
22:20:09,859 INFO [STDOUT] <xs:schema version=»1.0″ xmlns:xs=»http://www.w3.org/2001/XMLSchema«>
22:20:09,859 INFO [STDOUT]
22:20:09,906 INFO [STDOUT] <xs:element name=»report» type=»report»/>
22:20:09,906 INFO [STDOUT]
22:20:09,906 INFO [STDOUT] <xs:complexType name=»employeeDetails»>
22:20:09,906 INFO [STDOUT] <xs:sequence>
22:20:09,906 INFO [STDOUT] <xs:element name=»employeeId» type=»xs:long» minOccurs=»0″/>
22:20:09,906 INFO [STDOUT] <xs:element name=»firstName» type=»xs:string» minOccurs=»0″/>
22:20:09,906 INFO [STDOUT] <xs:element name=»lastName» type=»xs:string» minOccurs=»0″/>
22:20:09,906 INFO [STDOUT] <xs:element name=»departmentName» type=»xs:string» minOccurs=»0″/>
22:20:09,906 INFO [STDOUT] <xs:element name=»jobTitle» type=»xs:string» minOccurs=»0″/>
22:20:09,906 INFO [STDOUT] <xs:element name=»salary» type=»xs:float» minOccurs=»0″/>
22:20:09,906 INFO [STDOUT] </xs:sequence>
22:20:09,906 INFO [STDOUT] </xs:complexType>
22:20:09,906 INFO [STDOUT]
22:20:09,906 INFO [STDOUT] <xs:complexType name=»report»>
22:20:09,921 INFO [STDOUT] <xs:sequence>
22:20:09,921 INFO [STDOUT] <xs:element name=»employee» type=»employeeDetails» minOccurs=»0″ maxOccurs=»unbounded»/>
22:20:09,921 INFO [STDOUT] </xs:sequence>
22:20:09,921 INFO [STDOUT] </xs:complexType>
22:20:09,921 INFO [STDOUT] </xs:schema>
2. standalone client.
{code:xml}
<?xml version=»1.0″ encoding=»UTF-8″ standalone=»yes»?>
<xs:schema version=»1.0″ xmlns:xs=»http://www.w3.org/2001/XMLSchema«>
<xs:element name=»report» type=»report»/>
<xs:complexType name=»report»>
<xs:sequence>
<xs:element name=»employee» type=»employeeDetails» minOccurs=»0″ maxOccurs=»unbounded»/>
</xs:sequence>
</xs:complexType>
<xs:complexType name=»employeeDetails»>
<xs:sequence>
<xs:element name=»employeeId» type=»xs:long» minOccurs=»0″/>
<xs:element name=»firstName» type=»xs:string» minOccurs=»0″/>
<xs:element name=»lastName» type=»xs:string» minOccurs=»0″/>
<xs:element name=»departmentName» type=»xs:string» minOccurs=»0″/>
<xs:element name=»jobTitle» type=»xs:string» minOccurs=»0″/>
<xs:element name=»salary» type=»xs:float» minOccurs=»0″/>
</xs:sequence>
</xs:complexType>
</xs:schema>
{code:xml}
Why is it that the marshalling in the servlet throws an Exception and the marshalling in the standalone client gives a result like this:
{code:xml}
<?xml version=»1.0″ encoding=»UTF-8″ standalone=»yes»?>
<report>
<employee>
<employeeId>205</employeeId>
<firstName>Shelley</firstName>
<lastName>Higgins</lastName>
<departmentName>Accounting</departmentName>
<jobTitle>Accounting Manager</jobTitle>
<salary>12000.0</salary>
</employee>
<employee>
<employeeId>206</employeeId>
<firstName>William</firstName>
<lastName>Gietz</lastName>
<departmentName>Accounting</departmentName>
<jobTitle>Public Accountant</jobTitle>
<salary>8300.0</salary>
</employee>
<employee>
<employeeId>200</employeeId>
<firstName>Jennifer</firstName>
<lastName>Whalen</lastName>
<departmentName>Administration</departmentName>
<jobTitle>Administration Assistant</jobTitle>
<salary>4400.0</salary>
</employee>
…..
</report>
{code:xml}
Thanks in advance.