title: How best to implement link relations for HATEOAS in XML?
body: <p>We have a Java server web application where the core of the system contains a very complex domain model that was designed according to the principles of domain driven design.  For the most part, these domain objects have been affectly very little by the applications other concerns.  </p>
  
  <p>We are now looking to put a REST web services API in front of the system and I am struggling with how to best implement the HATEOAS links that come within our own new media type.  For example, lets say we have domain class <code>foo</code> that has an id and name properties with JAX-B annotations:</p>
  
  <pre><code>@XmlType(name = "foo")
  public class FooImpl implements Foo {
  
      private String name;
      private String id;
  
      ...snip....
  
  @XmlID
  @XmlAttribute
  @Override
  public String getId() {
      return id;
  }
  
      @XmlElement
      @Override
      public String getName() {
          return name;
      }
  
      @Override
      public void setName(final String name) {
          this.name = name;
      }
  }
  </code></pre>
  
  <p>But the XML I want to return looks like this:</p>
  
  <pre><code>&lt;foo id="123" href="http://myserver.com/foos/123"&gt;
     &lt;name&gt;myFoo&lt;/name&gt;
     &lt;links&gt;
            &lt;link rel="previous" href="http://myserver.com/foos/122" type="application/mything+xml" /&gt;
            &lt;link rel="next" href="http://myserver.com/foos/124" type="application/mything+xml" /&gt;
            &lt;link rel="edit" href="http://myserver.com/foos/123" type="application/mything+xml" /&gt;
            &lt;link rel="revise" href="http://myserver.com/foos/123" method="put" type="application/mything+xml" /&gt;
            &lt;link rel="cancel" href="http://myserver.com/foos/123?op="cancel"" method="post" type="application/mything+xml" /&gt;
     &lt;/links&gt;
  &lt;/foo&gt;
  </code></pre>
  
  <p>What is the best way to do this such that I do not have pollute my domain design with these media type links, but can still use the power of JAX-B for XML marshalling?   Here are some thoughts:</p>
  
  <p>1) <strong>JAX-B Adapters</strong> - could I use these to modify the XML for the entities and insert the links..is it possible?  is it reasonable?  Any examples?</p>
  
  <p>2) <strong>DTO Layer</strong> - Create a new REST service layer that converts my domain objects in DTOs.  So far we been able to avoid the hassle of DTOs.  While this would provide total flexibility in what we return to the client, I am also not looking to create domain agnostic clients here.</p>
  
  <p>3) <strong>Link Headers</strong> - I really like this idea, but I don't think it would work (by itself) because sometimes our resources contain collections of sub-resources.  In this case the subresources would still have to be marshalled into XML that contains links/hrefs, etc.  So while link headers solves the problem to the top level type, it doesn't solve the entire problem.  Feel free to say otherwise!</p>
  
  <p>Is there another approach that will help me avoid DTOs and yet remain transparent to the domain model?</p>