Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to marshall java.lang.Exception using JAXB Marshaller?
    text
    copied!<p>I am trying to marshall java.lang.Exception, but without success. Here's my code - </p> <pre><code>import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class JAXBTester { public static void main(String[] args) throws Exception { TestReport report = new TestReport(); report.setReportLog("Tests successful."); File file = new File("TestReport.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(TestReport.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(report, file); } } </code></pre> <p>This is the class I want to marshall - </p> <pre><code>import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class TestReport { private String reportLog; private Exception exception; @XmlElement public void setReportLog(String reportLog) { this.reportLog = reportLog; } public String getReportLog() { return reportLog; } @XmlElement public void setException(Exception exception) { this.exception = exception; } public Exception getException() { return exception; } } </code></pre> <p>I get the following exception - </p> <pre><code>Exception in thread "main" com.sun.xml.internal.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.Exception at public java.lang.Exception TestReport.getException() at TestReport at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:451) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.&lt;init&gt;(JAXBContextImpl.java:283) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.&lt;init&gt;(JAXBContextImpl.java:126) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1142) at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:130) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:248) at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:235) at javax.xml.bind.ContextFinder.find(ContextFinder.java:445) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:637) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584) at JAXBTester.main(JAXBTester.java:14) </code></pre> <p>This is because I am trying to marshall java.lang.Exception. How to solve this problem?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload