我正在使用 JAXB 生成基于某些 XSD 模式的 java 类。对于一个元素,例如:
<xsd:element name="REC_LOC" type="xsd:string" minOccurs="1"/>
jaxb 生成以下代码:
@XmlElement(name = "REC_LOC", required = true)
protected String recloc;
public String getRECLOC() {
return recloc;
}
/**
* Sets the value of the recloc property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setRECLOC(String value) {
this.recloc = value;
}
问题是我们需要使用一些专有的 XML 工具,这些工具依赖于 getter/setter 方法的命名约定。例如,对于 REC_LOC 字段,他们希望使用名为 getRecLoc(String value) 和 setRecLoc() 的方法,而不是 getRECLOC()。
有没有办法自定义jaxb生成的方法名?
最佳答案
您可以使用 jaxb:property
customization 自定义属性名称。
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.1">
<bindings schemaLocation="schema.xsd" version="1.0" node="/xs:schema">
<bindings node="xs:complexType[@name='SOME_TYPE']">
<bindings node="xs:sequence/xs:element[@name='REC_LOC']">
<property name="RecLoc"/>
</bindings>
</bindings>
</bindings>
</bindings>
(未测试。)
另见:
关于java - 如何在 JAXB 中自定义属性名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27039514/