コードでこの問題に遭遇しました。宣言されたフィールドを除くすべてのフィールド@XmlValue
が null の場合、MOXy はそのフィールドをオブジェクト全体の唯一の値であるかのようにマーシャリングします。これは意図的な実装かもしれませんが、回避策があるかどうか疑問に思っています。注: 現在 eclipselink moxy を使用していますが、これはすべての jaxb バインディングの標準のようです。
@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;
}
}
そして私はただ走っているだけです
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);
出力を
{
"foo" : {
value: "blah"
}
}
しかし、代わりに@XmlValue
注釈のためにそれは
{
"foo" : "blah"
}