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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
dbf
  • 415
  • 2
  • 10
  • 18

1 Answers1

1

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
  • 35,575
  • 15
  • 95
  • 119
  • 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 Feb 11 '10 at 08:52
  • 1
    @dbf you could also make these methods private. This requires explicitly annotating them with `@XmlElement` though. – sfussenegger Feb 11 '10 at 09:22
  • @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 Feb 11 '10 at 09:28
  • sfussenegger XmlJavaTypeAdapter is good, but I have a problem: it is not called during webservice method invocation. Does Aegis mapping support @XmlJavaTypeAdapter annotation? – dbf Feb 11 '10 at 13:33
  • sorry, can't help you with aegis – sfussenegger Feb 11 '10 at 13:38
  • 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 Feb 12 '10 at 05:43
  • Daniel, we don't use jaxb because with jaxb we get minOccurs="0" for all variables in classes. In aegis we can change it with and we can't find these options for jaxb. – dbf Feb 17 '10 at 12:25
  • @dbf isn't that what `@XmlElement(required = true)` is doing (on a per-property basis)? `@XmlElement(nillable = false)` is the default anyway. – sfussenegger Feb 17 '10 at 13:29
  • 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 Feb 17 '10 at 15:32