JAXB Bean 生成

2022-01-09 00:00:00 xsd schema java jaxb

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

I am using JAXB for generating beans from XSD's using a JAXB plugin in Maven. This is working fine, expect that the code contains isSetXXXXXX() methods for each field.

例如

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

for a field firstName, it is producing the following code:

@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 生成这些问题.

This isSetFirstName() method is causing issues and I don't want JAXB to generate these.

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

谢谢.

更新

解决了这个问题:问题出在 xjb 文件中,generateIsSetMethod 设置为 true.

Solved this: Problem was in the xjb file, generateIsSetMethod was set to 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>

这也回答了我的上一个问题.

推荐答案

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

By default a JAXB (JSR-222) implementation will not generate isSet methods. Since you are getting them one of the following must be true:

  1. 您可以在架构注释中指定:<jaxb:globalBindings generateIsSetMethod="true"/>
  2. 您有一个外部绑定文件指定:<jaxb:globalBindings generateIsSetMethod="true"/>
  3. 您正在为 Maven 插件指定一个属性以生成 isSet 方法.

相关文章