users@jersey.java.net

[Jersey] Re: Json output for subclasses

From: Adam Walczak <me_at_adamwalczak.info>
Date: Thu, 30 Dec 2010 16:38:12 +0100

The difference between the two is that the default ObjectMapper
ignores JAXB anntotations and does things in his own way and Jerseys
ObjectMapper uses an annotation introspector.

I understand the Staff class really looks like:

public class Staff extends Person { ...

Annotate the Staff class @XmlRootElement and do

    @XmlTransient
    public Long getRank() {
        return id;
    }

    @XmlElement(name="id")
    @XmlID
    public String getRankAsString() {
        return id.toString();
    }

or make the rank an Integer and create a:

public class NumberAdapter extends XmlAdapter<String, Number> { ...

and in the package-info.java do:

@XmlJavaTypeAdapters({
    @XmlJavaTypeAdapter(type=Date.class, value=DateAdaptor.class)
})
package your.package;

Yes I know this sucks :(

-- 
Adam Walczak
www.adamwalczak.info
+48 604 188 992