gpt4 book ai didi

jaxb2 - 如何使用 JAXB 自定义绑定(bind)自定义生成的 Java 对象的字段名称?

转载 作者:行者123 更新时间:2023-12-05 02:22:37 24 4
gpt4 key购买 nike

我正在尝试为以下示例 xsd 生成 JAXB 生成的对象。

<xs:complexType name="AddressType">   
<xs:sequence>
<xs:element name="USA" type="xs:string"/>
</xs:sequence>
</xs:complexType>

并且在没有任何自定义绑定(bind)的情况下生成的类是

@XmlAccessorType(XmlAccessType.FIELD)   
@XmlType(name = "AddressType", propOrder = {
"usa"
})
public class AddressType {

@XmlElement(name = "USA", required = true)
protected String usa;

/**
* Gets the value of the usa property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getUSA() {
return usa;
}

/**
* Sets the value of the usa property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setUSA(String value) {
this.usa = value;
}
}

如您所见,字段名称是“usa”,setters/getters 是 getUSA/setUSA。

是否有任何自定义设置/绑定(bind)也可以将字段名称生成为“USA”而不是“usa”,这样字段和属性都是“USA”。

我提到了 How to customize property name in JAXB?

但那是自定义属性,而不是字段..任何帮助

顺便说一下,我正在使用 maven-jaxb2-plugin

最佳答案

示例 xjb 文件,如您的情况:binding.xjb

例子:

<jaxb:bindings 
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.1">

<jaxb:bindings schemaLocation="schema.xsd">
<jaxb:bindings node="//xs:element[@name='USA']">
<jaxb:property name="usa" />
</jaxb:bindings>
</jaxb:bindings>

</jaxb:bindings>

在您的 xjc 命令中添加 -b binding.xjb 或在您的 maven xjc 插件中配置绑定(bind)文件位置。

关于jaxb2 - 如何使用 JAXB 自定义绑定(bind)自定义生成的 Java 对象的字段名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27976872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com