users@jersey.java.net

SV: [Jersey] Output formatting.

From: Wilhelmsen Tor Iver <TorIverW_at_arrive.no>
Date: Mon, 18 May 2009 20:51:11 +0200

---------
        When this has been marshalled into the XML file if the date
field is null, my output does not have that element in it. What I want
to do is include all the fields in the output; and if they are null,
replace them with - say a blank. So for "application/xml" the output
should be :

         

        <foo>

          <num>123</num>

          <date></date>

        </foo>


--------

You have the option of doing

        @XmlElement(nillable=true)
        public Date date = null;

This will (by default, using JAXB.marshal() for testing in my case) lead
to output like

        <date xsi:nil="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

which is not the format you want but at least it is there - and it is
more explicit about being null than just the "empty" case.

My test code:

@XmlRootElement(name="test")
@XmlAccessorType(XmlAccessType.FIELD)
public class JAXBTest {

        public int foo = 42;
        @XmlElement(nillable=true)
        public Date date = null;
        
        public static void main(String[] args) {
                JAXBTest object = new JAXBTest();
                StringWriter dest = new StringWriter();
                JAXB.marshal(object, dest);
                System.out.println(dest.toString());
        }
}

- Tor Iver