在使用jaxb2基础知识生成toString方法时,如何使用绑定文件排除字段

时间:2015-10-12 02:30:15

标签: java jaxb maven-jaxb2-plugin jaxb2-basics

old site上有一些文档,但目前还不清楚如何在使用绑定文件时应用配置。

这是我的maven配置,用于向生成的JAXB对象添加toString,equals等。

  <plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.12.3</version>
    <executions>
      <execution>
        <goals>
          <goal>generate</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <schemaIncludes>
        <include>*.xsd</include>
      </schemaIncludes>
      <args>
        <arg>-Xfluent-api</arg>
        <arg>-XautoNameResolution</arg>
        <arg>-XtoString</arg>
        <arg>-Xequals</arg>
        <arg>-XhashCode</arg>
        <arg>-Xcopyable</arg>
      </args>
      <plugins>
        <plugin>
          <groupId>org.jvnet.jaxb2_commons</groupId>
          <artifactId>jaxb2-fluent-api</artifactId>
          <version>3.0</version>
        </plugin>
        <plugin>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics</artifactId>
            <version>0.9.4</version>
        </plugin>
      </plugins>
    </configuration>
  </plugin>

1 个答案:

答案 0 :(得分:3)

将绑定到xsd的文件bindings.xjb添加到src/main/resources,选择相关元素,然后添加toString:ignored注释。

您还可以使用hashCode:ignore,或忽略所有插件basics:ignore

    <jaxb:bindings jaxb:version="1.0"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xjc= "http://java.sun.com/xml/ns/jaxb/xjc"
               xmlns:toString="http://jaxb2-commons.dev.java.net/basic/toString"
               xmlns:basic="http://jaxb2-commons.dev.java.net/basic"
               jaxb:extensionBindingPrefixes="basic copyable equals hashCode mergeable toString">

  <jaxb:bindings schemaLocation="PasswordPolicy.xsd">
    <jaxb:bindings node="xs:complexType[@name='ppValidatePolicyRequestType']//xs:element[@name='password']">
      <toString:ignored/>
    </jaxb:bindings>
  </jaxb:bindings>

  <jaxb:bindings schemaLocation="UserManagement.xsd">
    <jaxb:bindings node="xs:complexType[@name='umCreateUserRequestType']//xs:element[@name='password']">
      <toString:ignored/>
    </jaxb:bindings>
    <jaxb:bindings node="xs:complexType[@name='umUpdateUserRequestType']//xs:element[@name='password']">
      <toString:ignored/>
    </jaxb:bindings>
    <jaxb:bindings node="xs:complexType[@name='umResetUserPasswordRequestType']//xs:element[@name='password']">
      <toString:ignored/>
    </jaxb:bindings>
    <jaxb:bindings node="xs:complexType[@name='umCreateUserRequestType']//xs:element[@name='secretAnswer']">
      <toString:ignored/>
    </jaxb:bindings>
    <jaxb:bindings node="xs:complexType[@name='umUpdateUserRequestType']//xs:element[@name='secretAnswer']">
      <toString:ignored/>
    </jaxb:bindings>
  </jaxb:bindings>

</jaxb:bindings>

如果忽略toString,则在生成的类中,您现在应该看到从appendFields(...)方法中删除了适用的字段