Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@XmlJavaTypeAdapter w/ Inheritance

Tags:

java

jaxb

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.

like image 866
Dinesh Pillay Avatar asked Jun 24 '10 06:06

Dinesh Pillay


1 Answers

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

like image 58
Ross Judson Avatar answered Nov 01 '22 11:11

Ross Judson