【问题标题】:JAXB Bean GenerationJAXB Bean 生成
【发布时间】:2012-10-03 08:49:52
【问题描述】:

我正在使用 JAXB 使用 Maven 中的 JAXB 插件从 XSD 生成 bean。 这工作正常,希望代码包含每个字段的 isSetXXXXXX() 方法。

例如

对于字段 firstName,它会生成以下代码:

@XmlElement(name = "FirstName", required = true)
    protected String firstName;

  public String getFirstName() {
        return firstName;
    }

 public void setFirstName(String firstName) {
        this.token = firstName;
    }

    public boolean isSetFirstName() {
        return (this.firstName!= null);
    }

这个 isSetFirstName() 方法会导致问题,我不希望 JAXB 生成这些问题。

有没有办法阻止这种行为?

谢谢。

更新

已解决:问题出在 xjb 文件中,generateIsSetMethod 设置为 true。

<xs:annotation>
   <xs:appinfo>
      <jaxb:globalBindings generateIsSetMethod="true">

      bindingStyle="modelGroupBinding"
         choiceContentProperty="true" >

           <xjc:serializable uid="12343"/>
           <jaxb:javaType name="short" 
              xmlType="xs:long" 
              printMethod="javax.xml.bind.DatatypeConverter.printShort"   
              parseMethod="javax.xml.bind.DatatypeConverter.parseShort"/>

      </jaxb:globalBindings>
   </xs:appinfo>
</xs:annotation>

这也回答了我的previous question

【问题讨论】:

  • 嘿,阿迪,您更新的 XML sn-p 不是有效的 XML,generateIsSetMethod="true" 之后的 > 是否应该在那里?

标签: java xsd jaxb schema


【解决方案1】:

默认情况下,JAXB (JSR-222) 实现不会生成isSet 方法。由于您得到它们,因此必须满足以下条件之一:

  1. 您可以在模式注释中指定:&lt;jaxb:globalBindings generateIsSetMethod="true"/&gt;
  2. 您有一个外部绑定文件指定:&lt;jaxb:globalBindings generateIsSetMethod="true"/&gt;
  3. 您正在为 Maven 插件指定一个属性以生成 isSet 方法。

【讨论】:

  • 虽然我在你回答之前回答了我的问题,但这仍然是正确且内容丰富的答案,所以非常肯定。
猜你喜欢
  • 2021-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-29
  • 1970-01-01
  • 2011-09-20
  • 1970-01-01
相关资源
最近更新 更多