title: Spring DTO-DAO (Resource - entity) mapping goes in which application layer: Controller or Service?
body: <p>I'm writing a Spring (4.1.7) web application that exposes a RESTful service and wish to use DTO "resource" objects for communication between Controller and client browser rather than expose my persistence entities.</p>
  
  <p>Currently the application has the following layers:</p>
  
  <ul>
  <li>View (JSP/JSON)</li>
  <li>Controller(s)</li>
  <li>DAO (<code>@Service</code>)</li>
  <li>DAO (<code>@Repository</code>)</li>
  </ul>
  
  <p>My question is, where should I be mapping my DAO entities to DTO resources? I had a look at some examples using <code>Spring HATEOAS</code> and they show <code>Resource</code> objects extending <code>ResourceSupport</code> being mapped in the <code>Controller</code>. Is this the best way to do it, or should I be returning resources from the DAO Service?</p>
  
  <p>I wish to add <code>Link</code> elements to the returned resource (for self and related resources), but can't see how <code>Link</code> elements would be resolved if processed in the <code>Service</code> without it having knowledge of the <code>Controller</code> and it's <code>@RequestMapping</code>. On the other hand, I don't know if it's good practice to clutter the <code>Controller</code> with the mapping either.</p>