I've come across this problem in my code: when all fields except for the one declared @XmlValue
are null, MOXy marshals out the field as if it were the sole value of the whole object. I realize this may be a purposeful implementation but I'm wondering if there's any workaround to it. Note: I'm currently using eclipselink moxy, but this seems to be standard for all jaxb bindings
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class foo {
@XmlValue
private final String name;
@XmlAttribute(name = "name2", required = true)
private final String name2;
>....getters, setters, etc....<
public foo(String name, String name2) {
this.name = name;
this.name2 = name2;
}
}
and i'm simply running
JAXBContext jc = JAXBContext.newInstance(moxyTest.foo.class);
Marshaller marsh = jc.createMarshaller();
marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marsh.setProperty("eclipselink.media-type", "application/json");
StringWriter strWriter = new StringWriter();
foo example = new foo("blah", null);
marsh.marshal(example, strWriter);
i want my output to be
{
"foo" : {
value: "blah"
}
}
but instead due to the @XmlValue
annotation it's
{
"foo" : "blah"
}