2

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"
}
Milo Hou
  • 239
  • 2
  • 17

1 Answers1

1

UPDATE

This issue has now been fixed in the EclipseLink 2.5.1 and 2.6.0 streams. You can download a nightly build containing this fix from the following link starting August 8, 2013.


ORIGINAL ANSWER

I have opened the following bug for this issue. You can use it to track our progress.

bdoughan
  • 147,609
  • 23
  • 300
  • 400