Thursday, December 4, 2008

Return a tree of objects from a JAX-WS service

Here are the steps to return a tree of objects via a JAX-WS service:

Define the Node class with the necessary JAXB annotations:

import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Node {
  ...
  private List<Node> children;
  ...

  @XmlElementRef(type = Node.class)
  public List<Node> getChildren() {
    return children;
  }

  ...
}

Define the JAX-WS service method:

@WebMethod
public Node getTree() {
  ...
}

Deploy the service.

Use your IDE to generate the client proxy from WSDL. The client Node type will have:

public List<Node> getNode()

The accessor name changes as a result of the @XmlElementRef annotation.

No comments: