javaxmljaxbcxfaegis

Bean-To-XML annotations: how to process nested structure


For bean->xml convertion in webservices we use Aegis from CXF (it is jaxb-compatible, as I understand).

This is my type:

class C{
private int a;
private int b;
private T t;
...
}

class T{
private int t1;
private int t2;
}

I need t.t1 field to be on the same level in XML as a and b in C (bean restored from xml should be like this:

class C{ 
private int a; 
private int b;
private int t1 
}

(client code is interested only in field t1 from structure T). Thanks.


Solution

  • You could add getT1() and setT1(int) to C and make getT() @XmlTransient

    class C {
      // snip
    
      /**
       * JAXB only
       */
      @SuppressWarnings("unused")
      @XmlElement
      private void setT1(int t1) {
        if(t != null) {
          t.setT1(t1);
        } else {
          // TODO
        }
      }
    
      /**
       * JAXB only
       */
      @SuppressWarnings("unused")
      private int getT1() {
        if(t != null) {
          return t.getT1(t1);
        } else {
          // TODO
        }
      }
    }