Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are other ways of doing it, such as the one referenced by the commenter. But here's another way...</p> <p>The <code>Throwable</code> class in Java (superclass of <code>Exception</code>) is serializable in the sense of <code>java.io.Serializable</code>. This means that you can write it to a byte stream and then recompose it later from those bytes. (It's possible that your application might have poorly-written non-serializable subclasses of <code>Throwable</code>. If that's the case, the following won't work.)</p> <p>So one way to deal with this is to write a custom adapter that serializes the <code>Throwable</code> (or <code>Exception</code>) to bytes. And in the XML, you see the hex for those bytes. Then on the receiving end you can un-serialize and then work with (an exact replica of) the <code>Throwable</code> you started with.</p> <p>The bad part about this way is that the Exception is not human-readable inside the XML. The good part is that it's really simple. On your <code>TestReport</code> class, put this annotation on your <code>Exception</code> getter:</p> <pre><code>@XmlJavaTypeAdapter(ThrowableAdapter.class) public Exception getException() { return exception; } public void setException(Exception exception) { this.exception = exception; } </code></pre> <p>And then add this adapter class to your project:</p> <pre><code>import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import javax.xml.bind.annotation.adapters.HexBinaryAdapter; import javax.xml.bind.annotation.adapters.XmlAdapter; public class ThrowableAdapter extends XmlAdapter&lt;String, Throwable&gt; { private HexBinaryAdapter hexAdapter = new HexBinaryAdapter(); @Override public String marshal(Throwable v) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(v); oos.close(); byte[] serializedBytes = baos.toByteArray(); return hexAdapter.marshal(serializedBytes); } @Override public Throwable unmarshal(String v) throws Exception { byte[] serializedBytes = hexAdapter.unmarshal(v); ByteArrayInputStream bais = new ByteArrayInputStream(serializedBytes); ObjectInputStream ois = new ObjectInputStream(bais); Throwable result = (Throwable) ois.readObject(); return result; } } </code></pre> <p>Then your XML will contain an element like this:</p> <pre><code>&lt;exception&gt;AED...&lt;/exception&gt; </code></pre> <p>except in instead of <code>...</code> you'll see a huge hex string. When it's un-marshalled on the other side, it'll be just like the original.</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