1

旧站点上有一些文档, 但不清楚在使用绑定文件时如何应用配置。

这是我用来将 toString、equals 等添加到生成的 JAXB 对象的 maven 配置。

  <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>
4

1 回答 1

6

添加绑定到src/main/resourcesxsd 的文件 bindings.xjb,选择相关元素,然后添加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(...)方法中删除

于 2015-10-12T02:30:15.220 回答