views:

130

answers:

1

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.

+1  A: 

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
    }
  }
}
sfussenegger
Your solution probably works, but I don't want to add additional public methods to my class. It can confuse other developers, if they see two ways of reading/setting one variable c.getT().setT1(1), and c.setT1(). Of course, I can mark c.setT1() as deprecated or add comments, but if it is possible, I would like to avoid such code.
dbf
@dbf you could also make these methods private. This requires explicitly annotating them with `@XmlElement` though.
sfussenegger
@dbf another possibility would be to use `@XmlJavaTypeAdapter` and replace `C` with another object (could be a class that extends or wraps `C`) that contains the properties you want.
sfussenegger
sfussenegger XmlJavaTypeAdapter is good, but I have a problem: it is not called during webservice method invocation. Does Aegis mapping support @XmlJavaTypeAdapter annotation?
dbf
sorry, can't help you with aegis
sfussenegger
No, aegis doesn't support the type adapters. That's jaxb only. That would then bring up the question of why don't you just use jaxb?
Daniel Kulp
Daniel, we don't use jaxb because with jaxb we getminOccurs="0" for all variables in classes. In aegis we can change it with <property name="defaultNillable" value="false"/> <property name="defaultMinOccurs" value="1"/>and we can't find these options for jaxb.
dbf
@dbf isn't that what `@XmlElement(required = true)` is doing (on a per-property basis)? `@XmlElement(nillable = false)` is the default anyway.
sfussenegger
Yes, I want to set @XmlElement(required = true), but once in config file for all elements, not setting this annotation for EACh variable in EACH class.
dbf