users@glassfish.java.net

XmlElement.required=true is ignored by jaxb on marshalling

From: <glassfish_at_javadesktop.org>
Date: Thu, 30 Apr 2009 14:03:27 PDT

Hello,

Why XmlElement.required=true is ignored by jaxb on marshalling.
I expect to get something that looks like <bean><prop/></bean>

With nillable=true it works perfectly but I get nil attribute.
Is it possible to get just empty tag?

sincerely,
alexandre efremov

    @XmlAccessorType(XmlAccessType.PROPERTY)
    @XmlRootElement(name="bean")
    public static class Bean{
        private String prop;

        public void setProp(String prop) {
            this.prop = prop;
        }

        @XmlElement(required=true)
        public String getProp() {
            return prop;
        }
    }
  

    public void testJabxRequired() throws Exception{
        JAXBContext context=JAXBContext.newInstance(Bean.class);
        Marshaller marshaller = context.createMarshaller();
        Bean b=new Bean();
        StringWriter buffer=new StringWriter();
        marshaller.marshal(b, buffer);
        assertTrue(buffer.toString().endsWith("<bean/>"));
        Unmarshaller unmarshaller = context.createUnmarshaller();
        Bean clone=(Bean)unmarshaller.unmarshal(new StringReader(buffer.toString()));
        assertNull(clone.getProp());
    }
[Message sent by forum member 'aefremov' (aefremov)]

http://forums.java.net/jive/thread.jspa?messageID=344584