如何从JAXB生成的布尔getter中删除"is"

Kev*_*vin 7 java jaxb

JAXB的产生方法,如: isIsBuyNow()和isHasBuyNow()我想isBuyNow()还是hasBuyNow().

生成了许多类方法,因此单独定制生成的方法名称是不可行的.

用于自定义JAXB的某种正则规则是首先想到的,但我找不到任何更改方法名称的通用方法.

bdo*_*han 5

您可以使用外部绑定文件来自定义您的属性名称。为了让 JAXB 将方法识别为属性,它必须遵循以布尔类型开头get或is为布尔类型开头的约定。

架构.xsd

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

   <xs:complexType name="root">
      <xs:attribute name="is-buy-now" type="xs:boolean"/>
   </xs:complexType>

</xs:schema>
Run Code Online (Sandbox Code Playgroud)

绑定文件

在下面的绑定文件中,我们为名为 的 XML 属性指定了一个属性名称is-buy-now:

<jxb:bindings 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">
    <jxb:bindings schemaLocation="is.xsd">
        <jxb:bindings node="//xs:complexType[@name='root']/xs:attribute[@name='is-buy-now']">
        <jxb:property name="buyNow"/>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>
Run Code Online (Sandbox Code Playgroud)

XJC呼叫

该-b选项用于指定绑定文件:

xjc -d out -b bindings.xml schema.xsd
Run Code Online (Sandbox Code Playgroud)

根

结果将生成以下类:isBuyNow和setBuyNow方法:

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2012.02.03 at 05:18:59 AM EST 
//


package generated;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for root complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="root">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;attribute name="is-buy-now" type="{http://www.w3.org/2001/XMLSchema}boolean" />
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "root")
public class Root {

    @XmlAttribute(name = "is-buy-now")
    protected Boolean buyNow;

    /**
     * Gets the value of the buyNow property.
     * 
     * @return
     *     possible object is
     *     {@link Boolean }
     *     
     */
    public Boolean isBuyNow() {
        return buyNow;
    }

    /**
     * Sets the value of the buyNow property.
     * 
     * @param value
     *     allowed object is
     *     {@link Boolean }
     *     
     */
    public void setBuyNow(Boolean value) {
        this.buyNow = value;
    }

}
Run Code Online (Sandbox Code Playgroud)

更新

如果您从 Java 类开始,您可以指定@XmlAccessorType(XmlAccessType.FIELD)映射到字段并根据您的选择命名您的方法:

@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlAttribute(name="has-buy-now")
    private boolean buyNow;

    public boolean hasBuyNow() {
        return buyNow;
    }

}
Run Code Online (Sandbox Code Playgroud)


Kal*_*see 2

听起来您只需要更改方法名称,而不是类名称。您也许可以:

  1. 找到一个允许此操作的 XJC 插件,或者
  2. 编写一个 XJC 插件来执行此操作

我怀疑第一个选项是否存在(尽管你可能会幸运),但第二个选项似乎非常简单。请参阅此页面以获取有关如何编写自己的文章的一些帖子。