dan carter
dan carter

Reputation: 4351

How do i exclude a field using a bindings file when using jaxb2 basics to generate the toString method

There is some documentation at the old site but it is unclear how to apply the configuration when using a bindings file.

Here is my maven configuration that i use to add toString, equals etc to my generated JAXB objects.

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

Upvotes: 1

Views: 1923

Answers (1)

dan carter
dan carter

Reputation: 4351

Add a file bindings.xjb to src/main/resources which binds to your xsd, selects the relevant element and then add the toString:ignored annotation.

You can also use hashCode:ignore, or to ignore from all plugins 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>

If you are ignoring from toString, in the generated class you should now see the applicable field is removed from the appendFields(...) method

Upvotes: 6

Related Questions