自定义xjb非常适合根据需要覆盖名称,但是我们会丢失名称中的下划线。

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
  version="2.1">
  <jxb:globalBindings underscoreBinding="asCharInWord"/>
   <jxb:bindings schemaLocation="foo.xsd">
        <jxb:bindings node="//xs:complexType[@name='fooType']">
            <jxb:property name="value" />
        </jxb:bindings>
  </jxb:bindings>
</jxb:bindings>

如您所见,上面的xjb生成的Java代码是
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "textType", propOrder = {
"value"
})
public class FooType {

@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> value;
......
 public List<Object> getValue() {
    if (value == null) {
        value = new ArrayList<Object>();
    }
    return this.value;
}

现在,一旦我将上面xjb中的一行更改为:
 <jxb:property name="_value" />

Java代码中所有的更改是:
public List<Object> get_Value() {
    if (value == null) {
        value = new ArrayList<Object>();
    }
    return this.value;
}

观察到:“值”
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "textType", propOrder = {
"value"
})

期望:“_value”
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "textType", propOrder = {
"_value"
})

最佳答案

由于指定了propOrder且字段名称为@XmlAccessorType(XmlAccessType.FIELD),所以以下value很好,即使该属性称为_Value(请参见:http://blog.bdoughan.com/2012/02/jaxbs-xmltype-and-proporder.html)。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "textType", propOrder = {
"value"
})



您特定的_Value属性似乎可以保存任何内容。您如何希望将内容呈现为JSON?
@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> value;

10-05 23:01