tags:

views:

64

answers:

2

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.

A: 

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.

EdC
A: 

Looks like you need an @XmlElementRef on your T field, to tell JAXB to look that up dynamically.

Ross Judson