问题
The custom xjb works great for overriding the names as desired however we lose the underscores in the names.
<?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>
As you can see for the above xjb the java code generated is
@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;
}
Now, once I change one line in the xjb above to:
<jxb:property name="_value" />
All that changes in the java code is :
public List<Object> get_Value() {
if (value == null) {
value = new ArrayList<Object>();
}
return this.value;
}
Observed: "value"
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "textType", propOrder = {
"value"
})
Desired: "_value"
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "textType", propOrder = {
"_value"
})
回答1:
The following propOrder
is fine since @XmlAccessorType(XmlAccessType.FIELD)
is specified and the name of the field is value
even though the property is called _Value (see: http://blog.bdoughan.com/2012/02/jaxbs-xmltype-and-proporder.html).
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "textType", propOrder = {
"value"
})
The goal is to have the "value" appear as "_value" in my json
Your particular _Value
property appears to be to be able to hold anything. How do you want the contents to be rendered to JSON?
@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> value;
来源:https://stackoverflow.com/questions/16821060/override-the-jaxb-property-name-using-a-xjb-preserving-the-underscores