Ignoring a Java property

Often the Java interface we are using will have convenience methods that appear to be additional properties but are actually just a simpler way of accessing another property. An example is shown as follows:

public class QuoteRequest implements Serializable {
  ...
  private String[] products;
  ...
  public void setProducts(String[] products) {
    this.products = products;
  }
  public String[] getProducts() {
    return products;
  }
  public String getProduct(int i) {
    ...
  }
  public String getProduct() {
    return getProduct(0);
  }
  public void setProduct(String product) {
    products = new String[1];
    products[0] = product;
    return;
  }
  ...
}

In this example, the getProduct and setProduct methods are just simplified interfaces to the getProducts and setProducts methods in the case where there is only one product required. We would not want to generate an XML element for both the product and the products properties of this bean, so in this recipe we will show how to hide an unwanted property (product).

Getting ready

We need to know the class name of the bean and the name of the property that we wish to remove from the XML mapping.

How to do it...

  1. Add an <xml-transient> element to the mapping file.

    In the OXM mapping file, we declare that a property is to be transient and not show up in the WSDL mapping by marking it as <xml-transient>, as shown in the following code snippet:

    <?xml version="1.0" encoding="UTF-8" ?>
    <xml-bindings ... >
      <java-types>
        <java-type name="soa.cookbook.QuoteRequest">
          <java-attributes>
            <!-- Can remove mappings by making them
                 transient via xml-transient element -->
            <xml-transient java-attribute="product"/>
          </java-attributes>
        </java-type>
      </java-types>
    </xml-bindings>

    The <java-type> name property is the Java class that has the property we want to remove. The <xml-transient> name property is the name of the property in the class.

  2. Save the mapping file.
  3. Remap the interface if necessary.

    If the interface has already been mapped, then it is necessary to regenerate the WSDL interface for the changes we have made to take effect. Do this by deleting the existing wire and then rewiring.

How it works...

<xml-transient> causes the named property to be ignored by the Java to XML converter. This means that no XML element will be generated for the given property.

There's more...

We may decide to ignore a property because it is not used by our particular use case. This can simplify our composite. Any mappings from XML to Java will cause the property to be initialized with a default value, usually null.

Ignoring a property may improve runtime performance because there is less work to do, and the resulting XML document will also be smaller.

The EXMMapping project in the code samples has a sample OXM file (mappings.xml) demonstrating this.

See also

  • The Ignoring missing elements with XSLT recipe in this chapter
  • The Ignoring missing elements with Assign recipe in this chapter
  • The Overriding mapping of EJB data to XML recipe in this chapter
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.77.63