0

I am trying to deserialize received Entity from JAX-RS web service, however can't manage to do it succesfully for reference fields. The format of xml passed is :

<?xml version="1.0" encoding="UTF-8"?>
<linkReaderImpl>
   <name>DPIa0Nffg0WEBCLIENTt0Nffg0</name>
   <source>DPIa0Nffg0</source>
   <destination>WEBCLIENTt0Nffg0</destination>
   <latency>0</latency>
   <throughput>0.0</throughput>
</linkReaderImpl>

and the LinkReaderImpl class looks like:

@XmlRootElement
@XmlAccessorType( XmlAccessType.FIELD)
public class LinkReaderImpl extends NamedEntityReaderImpl implements LinkReader, Serializable{

    @XmlElement @XmlIDREF
    private NodeReaderImpl source;

    @XmlElement @XmlIDREF
    private NodeReaderImpl destination;

    private int latency;

    private float throughput;

    public void setLatency(int latency) {
        this.latency = latency;
    }

    public void setThroughput(float throughput) {
        this.throughput = throughput;
    }


    public LinkReaderImpl() {
        super(null);
        this.source = null;
        this.destination = null;
        this.latency = 0;
        this.throughput = 0;
    }
    @Override
    public NodeReader getSourceNode() {
        return this.source;
    }


    public void setSource(NodeReaderImpl source) {
        this.source = source;
    }

    @Override
    public NodeReader getDestinationNode() {
        return this.destination;
    }

    public void setDestination(NodeReaderImpl destination) {
        this.destination = destination;
    }

    @Override
    public int getLatency() {
        return this.latency;
    }

    @Override
    public float getThroughput() {
        return this.throughput;
    }

}

When done with :

LinkReaderImpl returnReader = response.readEntity(LinkReaderImpl.class);

Elements like name, latency, throughput get successfully deserialized. However, source and destination always end up being null despite obviously being marshalled and unmarshalled as intended. Is it some simple problem or I completely misunderstood id/idref functionality?

Lupus
  • 33
  • 1
  • 8

1 Answers1

0

For this to work, NodeReaderImpl needs to have a property annotated with @XmlID and there should be the corresponding element in your XML. The XML you have posted does not have NodeReaderImpl elements with IDs DPIa0Nffg0 or WEBCLIENTt0Nffg0. So JAXB can't find referenced object and assigns null to source and destination properties.

For more on @XmlID/@XmlIDREF see:

http://blog.bdoughan.com/2010/10/jaxb-and-shared-references-xmlid-and.html

You may also use the IDResolver, see:

https://stackoverflow.com/a/26607053/303810

lexicore
  • 42,748
  • 17
  • 132
  • 221