问题
I am trying to marshal a class which extends Thread to XML, the code is as follows:
@XmlRootElement
public class TransitionXML extends Thread {
ArcXML[] preArcs;
ArcXML[] postArcs;
int[] preArcTokens;
int[] postArcTokens;
int leastTime;
int longestTime;
String name;
//some methods
//a lot of get-set
@Override
public void run() {
while (true) {
if (this.postTransition() & this.preTransition()) {
//We take out the tokens.
this.executePreTranstion();
this.checkPreTransition();
try {
this.sleep(1000 * this.getTimeToPass());
} catch (InterruptedException ex) {
Logger.getLogger(TransitionXML.class.getName()).log(Level.SEVERE, null, ex);
}
//We see if we can put them in.
if (this.postTransition()) {
this.executePostTranstion();
this.checkPostTransition();
}
} else {
try {
this.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(TransitionXML.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
public TransitionXML()
{
}
}
}
Which leads me to this error: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
java.lang.Thread$UncaughtExceptionHandler is an interface, and JAXB can't handle interfaces. ... java.lang.Thread$UncaughtExceptionHandler does not have a no-arg default constructor.
If I understood this correctly the very fact that it extends Thread causes this problem but I haven't seen a solution on the net.
Does anyone know of a solution or work-around for this, besides making the class a object which implements runnable?
回答1:
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
If you use MOXy as your JAXB provider then you can use your TransitionXML
class as is. This is because MOXy will not attempt to create metadata for classes in a java
or javax
package such as java.lang.Thread
.
You can obtain the MOXy binary from the following location:
- http://www.eclipse.org/eclipselink/downloads/
To specify MOXy as your JAXB provider you need to put a file called jaxb.properties
in the same package as your domain classes with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
来源:https://stackoverflow.com/questions/13240512/marshalling-an-active-object-with-jaxb