Reputation: 93
I have an XmlJavaTypeAdapter
defined for each Exception in my exception heirarchy. I use a wrapper object for marshaling the exceptions as below:-
@XmlRootElement
public Wrapper<T extends BaseException> {
T exception;
}
The exceptions:-
@XmlJavaTypeAdapter(BaseExceptionAdapter.class) {
public class BaseException extends RuntimeException {
}
@XmlJavaTypeAdapter(DerivedExceptionAdapter.class) {
public class DerivedException extends BaseException {
}
When I try marshaling a wrapper object, JAXB by default always calls the BaseExceptionAdapter
even if the actual exception is of type DerivedException
. How can I force it to look for the instance type of the exception rather than the reference type.
Just to add, package-info
/ jaxb.index
etc are as excepted.
Upvotes: 7
Views: 806
Reputation: 1142
Looks like you need an @XmlElementRef on your T field, to tell JAXB to look that up dynamically.
Upvotes: 1
Reputation: 2349
Do you have all your exception sub-types listed in package-info / jaxb.index / newInitialContext(...)?
JAX-B will look at the instance type, but I believe the sub-types need to be registered with JAX-B. It won't discover the XMLJavaTypeAdapter annotation at runtime you have to explicitly register each sub-class with JaxB.
This can also be accomplished with the @XmlSeeAlso annotation from a class that is registered with JAX-B.
Upvotes: 0