JSON with JAXB and Jersey and inheritance

I am currently trying to serialize some complex objects to JSON using JAXB. I am using Jersey as a REST framework with the following configuration class:

@ApplicationPath("/customerManagement")
public class ApplicationConfiguration extends ResourceConfig {

    public PartyLocationManagementApplication() {
        register(MOXyJsonProvider.class);
    }
}

      

I have installed the following classes:

@XmlType
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({
        AbstractListRTO.class,
        AbstractDataRTO.class
})
public class RestTransferObject {
    private Link self;
    public Link getSelf() {
        return self;
    }
    public void setSelf(Link self) {
        this.self = self;
    }
}

@XmlType
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({CustomersRTO.class})
public abstract class AbstractListRTO<T extends RestTransferObject>
extends RestTransferObject {

    @XmlElement(name = "element")
    private Collection<T> elements;
    public Collection<T> getElements() {
        return elements;
    }
    public void setElements(Collection<T> elements) {
        this.elements = elements;
    }
}

@XmlRootElement
@XmlType
@XmlAccessorType(XmlAccessType.FIELD)
public class CustomersRTO extends AbstractListRTO<CustomerRTO> {

    public CustomersRTO(Collection<CustomerRTO> customers) {
        super.setElements(customers);
    }
    public CustomersRTO() {
    }
    public Collection<CustomerRTO> getCustomers() {
        return getElements();
    }
    public void setCustomers(Collection<CustomerRTO> customers) {
        setElements(customers);
    }
}

@XmlType
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({CustomerRTO.class})
public abstract class AbstractDataRTO extends RestTransferObject {
    private Integer no;
    public AbstractDataRTO() {
    }
    public Integer getNo() {
        return no;
    }
    public void setNo(Integer no) {
        this.no = no;
    }
}

@XmlRootElement
@XmlType
@XmlAccessorType(XmlAccessType.FIELD)
public class CustomerRTO extends AbstractDataRTO {

    public CustomerRTO() {
    }
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

      

I am currently getting the following JSON output:

{
"@type": "customersRTO",
"element": [
    {
        "@type": "abstractDataRTO",
        "self": {
            "href": "http://somelink/1"
        },
        "no": 1
    },
    {
        "@type": "abstractDataRTO",
        "self": {
            "href": "http://somelink/1"
        },
        "no": 2
    }
]
}

      

but I want something like this:

{
"@type": "customersRTO",
"element": [
    {
        "@type": "customerRTO",
        "self": {
            "href": "http://somelink/1"
        },
        "no": 1,
        "name": "Jon"
    },
    {
        "@type": "customerRTO",
        "self": {
        "self": {
            "href": "http://somelink/1"
        },
        "no": 2,
        "name": "Jane"
    }
]
}

      

As you can see, @type fields have different meanings and there is no field name. I made sure to add the CustomerRTO to the CustomerRTO list. If I ask for the xml representation everything works fine, but as soon as I try to request the JSON representation, things go wrong.

+3


source to share





All Articles